Snippets Collections
// models.py
class Product(models.Model):
    title = models.CharField(max_length=225, unique=True)
    slug = models.SlugField(max_length=225, unique=True, null=True)

// admin.py
class ProductAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

// views.py
class productDetail(generic.DetailView):
    model = Detail
    template_name = 'detail.html'

// urls.py
    path('product/<slug:slug>/', views.productDetail.as_view(), name='productDetail'),
// create a schema
const eventSchema = new Schema({
  name: String,
  slug: {
    type: String,
    unique: true
  },
  description: String
});

// create the model
const eventModel = mongoose.model('Event', eventSchema);

// middleware -----
// make sure that the slug is created from the name
eventSchema.pre('save', function(next) {
  this.slug = slugify(this.name);
  next();
});

// function to slugify a name
function slugify(text) {
  return text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '');            // Trim - from end of text
}
star

Mon Oct 11 2021 04:11:32 GMT+0000 (UTC)

#django #ecommerce #slug
star

Mon Jun 14 2021 12:21:17 GMT+0000 (UTC) https://scotch.io/courses/create-a-crud-app-with-node-and-mongodb/a-mongoose-model

#express #nodej #mongodb #mongoose #slug #url

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension