0% found this document useful (0 votes)
72 views3 pages

Django Models and URL Design Guide

The document provides documentation on designing models, views, templates, and applications in Django. It describes creating a Reporter and Article model with fields like pub_date and headline, generating migrations, registering the model with the admin site, designing URLs and views to query and display the Article model data by year, and creating base and detail templates to render the year archive. It also covers starting a project with startproject, running the development server, and creating the Polls app with startapp.

Uploaded by

Duke MUge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views3 pages

Django Models and URL Design Guide

The document provides documentation on designing models, views, templates, and applications in Django. It describes creating a Reporter and Article model with fields like pub_date and headline, generating migrations, registering the model with the admin site, designing URLs and views to query and display the Article model data by year, and creating base and detail templates to render the year archive. It also covers starting a project with startproject, running the development server, and creating the Polls app with startapp.

Uploaded by

Duke MUge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

DJANGO DOCUMENTATION

designing a model
from django.db import models
class Reporter(models.Model):
full_name = models.CharField(max_length=70)

def __str__(self):
return self.full_name

class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

def __str__(self):
return self.headline

install
$ python manage.py makemigrations
$ python manage.py migrate

REGISTER MODEL
from django.contrib import admin
from . import models
admin.site.register(models.Article)

2.1.5 Design your URLs


To design URLs for an app, you create a Python module called a URLconf .
from django.urls import path

from . import views

urlpatterns = [
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<int:pk>/', views.article_detail),
]

Listing 5: mysite/news/views.py
from django.shortcuts import render
from .models import Article

def year_archive(request, year):


a_list = Article.objects.filter(pub_date__year=year)
context = {'year': year, 'article_list': a_list}
return render(request, 'news/year_archive.html', context)
2.1.7 Design your templates

Listing 6: mysite/news/templates/news/year_archive.html

{% extends "base.html" %}
{% block title %}Articles for {{ year }}{% endblock %}
{% block content %}

<h1>Articles for {{ year }}</h1>

{% for article in article_list %}


<p>{{ article.headline }}</p>
<p>By {{ article.reporter.full_name }}</p>
<p>Published {{ article.pub_date|date:"F j, Y" }}</p>
{% endfor %}
{% endblock %}

Listing 7: mysite/templates/base.html
{% load static %}
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<img src="{% static "images/sitelogo.png" %}" alt="Logo">
{% block content %}{% endblock %}
</body>
</html>

2.3.1 Creating a project


$ django-admin startproject mysite

Let’s look at what startproject created:


mysite/
manage.py
mysite
__init__.py
settings.py
urls.py
asgi.py
wsgi.py

2.3.2 The development server


$ python manage.py runserver

2.3.3 Creating the Polls app

$ python manage.py startapp polls

That’ll create a directory polls, which is laid out like this:

polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

This directory structure will house the poll application.

2.4.5 Introducing the Django Admin

Listing 21: polls/views.py

from django.shortcuts import render


from .models import Question

def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)

You might also like