Runserver, Pytz, Views - JeongtaekLim/TIL GitHub Wiki

Contents

  • Run server
  • Pytz
  • Views

In privious lecture, we studied how to make simple blog application to basic project. In this time, we will figure it out in detail ourselves.

As you already know, an app require below things for acting with user.

  • Model
  • Views
  • Urls
  • Templates(+template language)
  • Etc(ex. admin)

In this day, I'll follow up again these things myself as possible as without book.

Tips

  • If you want to run a test server on another port, just add port number as parameter of runserver command.
$ python manage.py runserver 7999
  • If you want to run server on other address, below is also ok
$ python manage.py runserver 0.0.0.0:7999
  • What is 'unique_for_date'? Set this to the name of a DateField or DateTimeField to require that this field be unique for the value of the date field. For example, if you have a field title that has unique_for_date="pub_date", then Django wouldn’t allow the entry of two records with the same title and pub_date.

  • Datetime Since in blog project, we have to deal with datetimes, so we have to install pytz

$ pip install pytz
  • Localization If you want to change language in admin site, then in settings.py
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'ko'
  • Migrations If you want to add new table and get message like 'there is no changes in model'
  1. Delete migration information on table and
DELETE FROM django_migrations WHERE app='blog'
  1. Just do migrate again
$ python manage.py makemigration blog
$ python manage.py migrate blog

Models

  1. Write models.py
  2. Make migration
  3. Migrate

Views - by function

There are two ways to make view. First one is definition of function, Second one is definition of class At first, we just adjust first one(by function)

  1. Write down views.py
# for simplest example
def post_list(request):
	posts = Post.published.all()
	return render(request, 'blog/post/list.html', {'posts': posts})
  1. Designate url to view in urls.py with regex inside of 'blog' application. If you want to study regex, please look https://docs.python.org/3/howto/regex.html
url(r'^$', views.post_list, name='post_list'),
  1. Designate url from 'mysite' application to your 'blog' application
url(r'^blog/', include('blog.urls',
    						namespace = 'blog',
    						app_name = 'blog')),

Views - by class

Actually, in case of list view, you can use django's ListView class for implementing list view

  • views.py
class PostListView(ListView):
	queryset = Post.published.all()
	context_object_name = 'posts'
	paginate_by = 3
	template_name = 'blog/post/list.html'
  • list.html you have to modify last line into page = page_obj
{% include "blog/pagination.html" with page=page_obj %}

Etc

  1. Admin account after once you made models into your database, you may want to make new superuser account.
$ python manage.py createsuperuser
  1. Adding your models to the administration site In admin.py,
from django.contrib import admin
from .models import Post
admin.site.register(Post)