ASSIGNMENT - 08 BLOG CREATION USING DJANGO
AIM:
To create a blog using Django.
PROCEDURE: INSTALLING DJANGO
Step 1: Create a virtual environment myproject using the command python -m venv myproject.
Step 2: Navigate to Scripts folder and enter into the environment using the command [Link].
Step 3: Install Django using the command python -m pip install Django.
Step 4: Come out of scripts folder and create a project with the same name as that of the virtual
environment using django-admin startproject myproject.
Step 5: Move to the sub myproject folder and start the server using python [Link] runserver.
BLOG CREATION:
Step 1: Create an app named boards using django-admin startapp boards.
Step 2: In myproject\myproject\myproject\[Link], include ‘boards’ inside the installed_apps list.
[Link]:
INSTALLED_APPS = [
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'boards',
Step 3: Let’s create our first view. In myproject\myproject\boards\[Link], enter any message.
[Link]
from [Link] import render
# Create your views here.
from [Link] import HttpResponse
def home(request):
return HttpResponse('Hello all!')
Step 4: To call the view, we need to map it to the myproject\myproject\myproject\[Link]. Then run
the server using python [Link] runserver.
[Link]
from [Link] import url
from [Link] import admin
from boards import views
from [Link] import path
urlpatterns = [
url(r'^$', [Link], name='home'),
url(r'^admin/', [Link]),
Step 5: To create the blog page, in myproject\myproject\boards\[Link], Create a class named
BlogPost with the variables to create title,body and timestamp for our blog.
[Link]:
from [Link] import models
# Create your models here.
class BlogPost([Link]):
title = [Link](max_length=150)
body = [Link]()
timestamp = [Link]()
SETTING UP THE DATABASE:
Step 6: Install mysql-connector and mysqlclient.
Step 7: In myproject\myproject\myproject\[Link], change the database connectivity from sqlite3 to
mysql and create a database named sample.
[Link]
DATABASES = {
'default': {
'ENGINE': '[Link]',
'NAME': 'sample',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '',
Step 8: Using the commands python [Link] makemigrations; python [Link] migrate to save the
current changes and check if the XAMPP is connected.
CREATING A SUPER USER
Step 9: In myproject\myproject\myproject\[Link], include the url for admin page.
[Link]
from [Link] import url
from [Link] import admin
from boards import views
from [Link] import path
urlpatterns = [
url(r'^$', [Link], name='home'),
url(r'^admin/', [Link]),
]
[Link]()
Step 10: Using the command python [Link] createsuperuser, create a new superuser by giving
the username, email and password.
Step 11: To include the user registration feature, in myproject\myproject\boards\[Link],
include the following.
[Link]
from [Link] import admin
# Register your models here.
from boards import models
# Register your models here.
class BlogPostAdmin([Link]):
list_display=('title','timestamp')
[Link]([Link], BlogPostAdmin)
Step 12: Using the commands python [Link] makemigrations; python [Link] migrate to save
the current changes.
Step 13: Run the server using the command python [Link] runserver.
Step 14: Go to this link [Link] to view the blog page.
Step 15: Now let’s register a new user.
Step 16: Now let’s login using our registered user.
Step 17: Let’s start creating our blog.
RESULT:
Thus, successfully created the blog using Django.