Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python django adding category

// go to the editor terminal 
python manage.py startapp category
// add to settings.py 
INSTALLED_APPS = [
    'category.apps.CategoryConfig',
]
// go to category folder app - models.py 
from django.db import models

//Create your models here.
class Category(models.Model):
    category_name   = models.CharField(max_length=50, unique=True)
    slug            = models.SlugField(max_length=100, unique=True)
    description     = models.TextField(max_length=255, blank=True)
    cat_image       = models.ImageField(upload_to='photos/category', blank=True)

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.category_name

// go to category folder app - admin.py 
from django.contrib import admin
from .models import Category

//Register your models here.
class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('category_name',)}
    list_display = ('category_name', 'slug')

admin.site.register(Category, CategoryAdmin)
// go to editor terminal trying check errors 
python manage.py runserver 
// making migrations 
python manage.py makemigrations
// migrating 
python manage.py migrate
// check to your /admin/ if refelected 
 
PREVIOUS NEXT
Tagged: #python #django #adding #category
ADD COMMENT
Topic
Name
7+1 =