[PYTHON] DJango Memo: From the beginning (creating a view)

Tutorial 3 started. From this time, you will learn how to create a public page.

Write URLConf settings

The following description is required, but it is created automatically at the start project. setting.py ######

ROOT_URLCONF = 'mysite.urls'

The same applies to URLConf in urls.py. First, add a pattern here.

urls.py ######

from django.conf.urls import patterns, include, url  #add to

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # url(r ‘URL pattern’,‘Corresponding view function’)How to write
    url(r'^polls/$', 'polls.views.index'),  #add to
    url(r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),  #add to
    url(r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),  #add to
    url(r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),  #add to
    url(r'^admin/', include(admin.site.urls))
) 
I googled because I don't understand the regular expression at all.

Basic: http://www.mnet.ne.jp/~nakama/ Python:http://docs.python.jp/2.7/library/re.html#module-re

It seems that ^ means the start point and $ means the end point, so

As for ‘(? P <poll_id> \ d +)’, if you try to disassemble it from what you know,

This is used when evaluating multiple characters at once.

It seems that the following corresponding character strings are assigned to poll_id.

It seems that this can also be written as'[0-9]'.

In other words, when it comes to ‘\ d +’ collectively, one or more half-width numbers are applicable.

For the time being, all patterns are changed from ‘^’ to ‘$’, so the expression specified inside it will be excluded.

Make a view

views.py ######

from django.http import HttpResponse

def index(request):  # index.html-like role
    return HttpResponse("Hello, world. You're at the poll index.")

def detail(request, poll_id):  # url:/polls/1/Display with
    return HttpResponse("You're looking at poll %s." % poll_id)
def results(request, poll_id):  # url:/polls/1/results/Display with
    return HttpResponse("You're looking at the results of poll %s." % poll_id)
def vote(request, poll_id):  # url:/polls/1/vote/Display with
    return HttpResponse("You're voting on poll %s." % poll_id)

Functions after detail have poll_id as the first argument. In the previous url (r'URL pattern','view function'), if the URL matches the regular expression, it seems that the value assigned to'? P \ <poll_id>' is passed to the second argument of the view function. (Since they were'\ d +', they are half-width numbers with one or more digits. If it is'url: / pols / 1 /', it is '1'). ** I changed the name and it didn't work, so I may have acquired it as a variable. ** **

When I accessed each URL, it was displayed properly.

Recommended Posts

DJango Memo: From the beginning (creating a view)
DJango Note: From the beginning (creating a view from a template)
DJango Memo: From the beginning (preparation)
DJango Note: From the beginning (using a generic view)
DJango Memo: From the beginning (model settings)
DJango Memo: From the beginning (Error screen settings)
DJango memo: From the beginning (editing the management screen) There is a mystery
DJango Memo: From the beginning (more edits to the management screen)
DJango Note: From the beginning (form processing)
DJango memo: From the beginning (using the management screen) my addictive point
Django memo # 1 from scratch
Steps from installing Python 3 to creating a Django app
DJango Note: From the beginning (simplification and splitting of URLConf)
The story of a Django model field disappearing from a class
Django: Import a class from a string
Get the value from the [Django] Form
[Python] Creating a scraping tool Memo
Learning notes from the beginning of Python 1
Commands for creating a new django project
Creating a login screen with Django allauth
Memo for creating a text formatting tool
How to get a namespaced view name from a URL (path_info) in Django
[Django] A memo when log was not
Use Django from a local Python script
A memo explaining the axis specification of axis
Memo about Sphinx Part 1 (Creating a project)
Learning notes from the beginning of Python 2
Get only the text from the Django form.
The story of Django creating a library that might be a little more useful
[Django] Create a form that automatically fills in the address from the zip code
A memo when creating a python environment with miniconda
Try modifying the TortoiseHg file view a bit
Finding the beginning of Abenomics from NT magnification 2
Specify the view URL in your Django template
Run a Python file from html using Django
A story of creating 16 * 16 dots from a Digimon photo
A memo for creating a python environment by a beginner
Finding the beginning of Abenomics from NT magnification 1
Django function-based view
Django Learning Memo
django tutorial memo
[Django] Hit a command you made from within the process that runs on manage.py.
Django class-based view
A memo to create a virtual environment (venv) before Django
[Python] Django Source Code Reading View Starting from Zero ①
The story of launching a Minecraft server from Discord
The wall of changing the Django service from Python 2.7 to Python 3
Became a human resources consultant from the help desk
Calculate volume from the two-dimensional structure of a compound
Learn Nim with Python (from the beginning of the year).
[GoLang] Set a space at the beginning of the comment
A memo to visually understand the axis of pandas.Panel
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Study from the beginning of Python Hour1: Hello World
Create a Todo app with the Django REST framework
Creating a graph using the plotly button and slider
Mathematical understanding of principal component analysis from the beginning
Python points from the perspective of a C programmer
A programming language that protects the people from NHK
Looking back on creating a web service with Django 2
Make the model a string on a Django HTML template