Python - Django Creating Models

Getting Started

First we're gonna start a new project named samplesite. If you have no idea how to create a new project you may refer to my tutorial Python Getting Started with Django.

Creating our App

Next, we're gonna create and install a new app named blog. If you have no idea on how to create and install a new app, you may visit my previous tutorial about Python - Django Creating New App.

Creating our URLs

Go to your main app, which in my case named samplesite folder and open urls.py and change the content to:
  1. from django.conf.urls import url, include
  2. from django.contrib import admin
  3.  
  4. urlpatterns = [
  5. url(r'^admin/', admin.site.urls),
  6. url(r'^', include('blog.urls')),
  7. ]
This will make our blog app as the index of our site.

Creating our Models

Next, we're gonna create our models which is the database of our app by opening our app directory, blog, then open models.py and paste the ff codes:
  1. from django.db import models
  2.  
  3. class Post(models.Model):
  4. title = models.CharField(max_length=140)
  5. body = models.TextField()
  6. date = models.DateTimeField()
  7.  
  8. def __str__(self):
  9. return self.title
In here, we have created table named Post with columns title, body and date. As you can see, we didn't create our id because django automatically does this for us.

Creating our Views

We're gonna create Generic View in this tutorial. Means that we are not going to use views.py to create our views. Open urls.py in our blog app directory and paste the ff codes:
  1. from django.conf.urls import url, include
  2. from django.views.generic import ListView, DetailView
  3. from blog.models import Post
  4.  
  5. urlpatterns = [
  6. url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],template_name="blog/blog.html"))
  7. ]

Creating the Pages of our App

First, in our blog app, we create templates directory. Then, inside templates, we create another directory named blog. This is to avoid conflicts between our templates. Then, create the following html files inside the blog. header.html
  1. <!DOCTYPE html>
  2. <title>Python - Django Templating</title>
  3. <meta charset="utf-8">
  4. {% load staticfiles %}
  5. <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type="text/css">
  6. </head>
  7. <div>
  8. {% block content %}
  9. {% endblock %}
  10. </div>
  11. </body>
  12. </html>
blog.html
  1. {% extends "blog/header.html" %}
  2.  
  3. {% block content %}
  4. <div class="container">
  5. <h2>Sample Table</h2>
  6. {% for post in object_list %}
  7. <div class="row">
  8. <div class="panel panel-default">
  9. <div class="panel-heading">
  10. {{post.title}}
  11.  
  12. </div>
  13. <div class="panel-body">
  14. <i>{{ post.date|date:"F d, Y h:i A" }}</i>
  15. <p>{{ post.body }}</p>
  16. </div>
  17. </div>
  18. </div>
  19. {% endfor %}
  20. </div>
  21. {% endblock %}
Note: Dont forget to create our static folder that contains our bootstrap. This is for our static files. The bootstrap is included in the source code of this tutorial. That's it for this tutorial. Please save this work for our next tutorial which will tackle about migration and admin. Happy Coding :)

Add new comment