Modify Admin Page - koglak/SWE573 GitHub Wiki

1) Activate Virtual Environment

 myvenv\Scripts\activate

2) Create new Application

 python manage.py startapp blog

image

3) Go to mysite/settings.py and add 'blog'.

You are saying Django that use new blog app!

 INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
 ]

4) Create first Model!

Enter below code to blog/models.py

 from django.conf import settings
 from django.db import models
 from django.utils import timezone

 class Post(models.Model):
     author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
     title = models.CharField(max_length=200)
     text = models.TextField()
     created_date = models.DateTimeField(default=timezone.now)
     published_date = models.DateTimeField(blank=True, null=True)

 def publish(self):
     self.published_date = timezone.now()
     self.save()

 def __str__(self):
     return self.title

5) Create your Database

 python manage.py makemigrations blog

6) Go to blog/admin.py and write below codes!

It is used to add,edit and delete posts! It will make our Post model in admin page!

 from django.contrib import admin

 from .models import Post

 admin.site.register(Post)

7) Make migrations!

 python manage.py makemigrations blog

8) Create Super User!

Create an account which has control over everything on site!

Don't panic when you could not type password!

python manage.py createsuperuser

9) Run server

python manage.py createsuperuser

10) Go to admin page!

Copy the route and paste it to web browser as below.

http://127.0.0.1:8000/admin/

image

11) Create new Posts!

image