We will create a news content on main page at our website.
- Go to on Command Prompt type cd Django01
- Activate virtual environment with type myvenv\Scripts\activate
- Type python manage.py startapp blog
- Register site on Setting.py file in \Django01\mysite with add script :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog'
] - Create images folder in \Django01\blog\blog folder.
- Open models.py file in folder \Django01\blog.
- Add the following script :
from django.db import models
from django.utils import timezone
class News(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
image=models.FileField(upload_to='images/')
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- Type python manage.py makemigrations blog
- Type python manage.py migrate blog