• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Python - Basic Comment System Using Django

Nyyriki

Chuckles Champion
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
66
Likes
59
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Basic Comment System Using Django. Django was created to achieve key objective and to ease the development of complicated, database-driven websites. It is a high-level Python Web framework with efficient programming capability . So let's now do the coding...

Getting Started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

After Python IDLE's is installed, open the command prompt then type "pip install Django", and hit enter.
2017-07-31_15_10_21-command_prompt_11.png

Creating the App

After django is set we will now create the web app. While your in the command prompt cd to the directory you want to save the app, then type "django-admin startproject server" and hit enter. A new folder will be created on the directory named 'server'.
2017-09-30_09_40_45-c_windows_system32_cmd.exe_0.png

Running The Server

After creating the project, cd to the newly created directory, then type "manage.py runserver" and hit enter to start the server running. The "manage.py" is a command of django-admin that utilize the administrative tasks of python web framework.

Here is the image of python web server:
2017-07-28_13_32_58-settings_11.png

Note: Type '127.0.0.1:8000' in the url browser to view the server. When there is code changes in the server just (ctrl + C) to command prompt to stop the server from running, then start again to avoid errors

Creating The Website

This time will now create the web app to display the web models. First locate the directory of the app via command prompt cd, then type "manage.py startapp blog" and hit enter. A new directory will be created named "blog".
2017-10-23_14_32_13-c_windows_system32_cmd.exe_.png

Setting up The URL

This time will now create a url address to connect the app to the server. First go to server directory, then open urls.py via Python IDLE's or any text editor. Then import "include" module beside the "url" module. After that copy/paste the code below inside the urlpatterns.
  1. url(

    r'^blog/'

    ,

    include(

    'blog.urls'

    )

    )

    ,
  2. url(

    r'^admin/'

    ,

    admin.site

    .urls

    )

    ,

It will be look like this:
  1. from

    django.conf

    .urls

    import

    url,

    include
  2. from

    django.contrib

    import

    admin

  3. urlpatterns =

    [
  4. url(

    r'^blog/'

    ,

    include(

    'blog.urls'

    )

    )

    ,
  5. url(

    r'^admin/'

    ,

    admin.site

    .urls

    )

    ,
  6. ]

Creating The Path For The Pages

Now that we are all set we will now create a path for the web pages. All you have to do first is to go to blog directory, then copy/paste the code below and save it inside "blog" directory named it as 'urls.py' The file name must be urls.py or else there will be an error in the code.
  1. from

    django.conf

    .urls

    import

    url
  2. from

    . import

    views

  3. urlpatterns =

    [
  4. url(

    r'^$'

    ,

    views.index

    ,

    name=

    'index'

    )

    ,
  5. url(

    r'^insert$'

    ,

    views.insert

    ,

    name=

    "insert"

    )
  6. ]

Creating A Static Folder

This time we will create a directory that store an external file. First go to the blog directory then create a directory called "static", after that create a sub directory called "blog". You'll notice that it is the same as your main app directory name to assure the absolute link. This is where you import the css, js, etc directory.

For the bootstrap framework you get it from here http://getbootstrap.com/
Creating The Views

The views contains the interface of the website. This is where you assign the html code for rendering it to django framework and contains a methods that call a specific functions. To do that open the views.py, the copy/paste the code below.
  1. from

    django.shortcuts

    import

    render,

    redirect
  2. from

    .models

    import

    Post

  3. # Create your views here.

  4. def

    index(

    request)

    :
  5. context =

    Post.objects

    .all

    (

    )
  6. return

    render(

    request,

    'blog/index.html'

    ,

    {

    'context'

    : context}

    )

  7. def

    insert(

    request)

    :
  8. post =

    Post(

    name=

    request.POST

    [

    'name'

    ]

    ,

    content=

    request.POST

    [

    'content'

    ]

    )
  9. post.save

    (

    )
  10. return

    redirect(

    '/blog/'

    )

Creating The Models

Now that we're done with the views we will then create a models. Models is module that will store the database information to django. To do that locate and go to blog directory, then open the "models.py" after that copy/paste the code.
  1. from

    django.db

    import

    models
  2. from

    datetime

    import

    datetime

  3. # Create your models here.
  4. class

    Post(

    models.Model

    )

    :
  5. name =

    models.CharField

    (

    max_length=

    80

    )
  6. content =

    models.CharField

    (

    max_length=

    255

    )
  7. date =

    models.DateTimeField

    (

    default=

    datetime

    .now

    )

  8. def

    __str__

    (

    self

    )

    :
  9. return

    self

    .name

Registering The App To The Server

Now that we created the interface we will now then register the app to the server. To do that go to the server directory, then open "settings.py" via Python IDLE's or any text editor. Then copy/paste this script inside the INSTALLED_APP variables 'blog'.

It will be like this:
  1. INSTALLED_APPS =

    [
  2. 'blog'

    ,
  3. 'django.contrib.admin'

    ,
  4. 'django.contrib.auth'

    ,
  5. 'django.contrib.contenttypes'

    ,
  6. 'django.contrib.sessions'

    ,
  7. 'django.contrib.messages'

    ,
  8. 'django.contrib.staticfiles'

    ,
  9. ]

Creating The Mark up Language

Now we will create the html interface for the django framework. First go to blog directory, then create a directory called "templates" and create a sub directory on it called blog.

base.html
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. {% load static %}
  6. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "{% static 'blog/css/bootstrap.css' %}"

    /

    >
  7. </

    head

    >
  8. <body

    >
  9. <nav class

    =

    "navbar navbar-default"

    >
  10. <div

    class

    =

    "container-fluid"

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  12. </

    div

    >
  13. </

    nav>
  14. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  15. <div

    class

    =

    "col-md-6 well"

    >
  16. <h3

    class

    =

    "text-primary"

    >

    Python - Basic Comment System Using Django </

    h3

    >
  17. <hr

    style

    =

    "border-top:1px dotted #000;"

    /

    >
  18. {% block body %}
  19. {% endblock %}
  20. </

    div

    >
  21. </

    body

    >
  22. </

    html

    >

Save it as "base.html" inside the blog directory "sub directory of templates".

index.html
  1. {% extends 'blog/base.html' %}
  2. {% block body %}
  3. <div

    class

    =

    "col-md-1"

    ></

    div

    >
  4. <div

    class

    =

    "col-md-10"

    >
  5. <form

    method

    =

    "POST"

    action

    =

    "insert"

    class

    =

    "form-inline"

    >
  6. {% csrf_token %}
  7. <label

    >

    Name</

    label

    >
  8. <br

    /

    >
  9. <input

    type

    =

    "text"

    class

    =

    "form-control"

    name

    =

    "name"

    required=

    "required"

    /

    >
  10. <br

    /

    ><br

    /

    >
  11. <textarea

    style

    =

    "width:100%; height:80px; resize:none;"

    class

    =

    "form-control"

    name

    =

    "content"

    required=

    "required"

    ></

    textarea

    >
  12. <br

    /

    ><br

    /

    >
  13. <button

    class

    =

    "btn btn-sm btn-primary pull-right"

    >

    button

    >
  14. </

    form

    >
  15. </

    div

    >
  16. <div

    style

    =

    "clear:both;"

    ></

    div

    >
  17. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  18. {% for post in context %}
  19. <div

    class

    =

    "col-md-2"

    ></

    div

    >
  20. <div

    class

    =

    "col-md-8"

    style

    =

    "background-color:#fff; box-shadow:0px 0px 3px 1px #ccc; word-wrap:break-word;"

    >
  21. <label

    >

    {{ post.name }}</

    label

    >
  22. <p

    style

    =

    "margin:5px;"

    >
  23. {{ post.content }}
  24. </

    p

    >
  25. <hr

    style

    =

    "border-top:1px solid #ccc;"

    /

    >
  26. {{ post.date }}
  27. </

    div

    >
  28. <br

    style

    =

    "clear:both;"

    /

    ><br

    /

    >
  29. {% endfor %}

  30. {% endblock %}

Save it as "index.html" inside the blog directory "sub directory of templates".

Migrating The App To The Server

Now that we're done in setting up all the necessary needed, we will now then make a migration and migrate the app to the server. To do that open the command prompt then cd to the "server" directory, then type "manage.py makemigrations" and hit enter. After that type again "manage.py migrate" then hit enter.
2017-10-23_15_37_32-c_windows_system32_cmd.exe_.png


There you have it we successfully created a Basic Comment System Using Django. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

344,676

344,684

Top