First Django development

I used Django, the popular Python framework, for the first time. This time, I referred to the official Tutorial.

Reference: Django Documentation

Django installation

This time, we'll use virtualenv to create a virtual environment for Django (Python version 3.5.1).

$ pyenv virtualenv 3.5.1 django

Move to the directory used during development and set the virtual environment you just created.

$ mkdir django
$ cd django
$ pyenv local django

Then install Django using pip.

$ pip install django

Check if it was installed properly.

$ python -c "import django; print(django.get_version())"
1.9.4

I think I was able to do it.

Create a Django project

Refer to Writing your first Django app, part1.

First, create a Django project with the name mysite.

$ django-admin startproject mysite

If you specify a path after the project name, it seems that you can create it in the specified location (https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-startproject) (I haven't tried it ).

Running this command created a directory called mysite.

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

Go to the mysite directory and start the server.

$ python manage.py runserver

After executing the command, access http://127.0.0.1:8000/ with a browser. The "Welcome to Django" page is displayed properly (I forgot to take a screenshot).

You can also change the IP address or port number by specifying port number or ʻIP address: port number after runserver` in the above command.

For the time being, quit the server with Ctrl-C.

Creating a Polls app

Next, we will make an app. First, create the source for creating the app. Execute the following command in the mysite directory.

$ python manage.py startapp polls

You now have a directory named polls.

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

I will actually write the code from now on.

Change views.py in the polls directory you just created as follows.

polls/views.py


from django.http import HttpResponse

def index(request):
	return HttpResponse("Hello, world. You're at the polls index.")

Create a view (look) when you access localhost: 8080 / polls with this file (you can't see it even if you access it yet). It seems that you need something called URLconf to see this. Next we will create this.

Create a new file called ʻurls.pyin thepolls` directory. Write the following code.

$ vim polls/urls.py

polls/urls.py


from django.conf.urls import url
from . import views

urlpatterns = [
	url(r'^$', views.index, name='index'),
]

Next, edit ʻurls.py in mysite, one level above polls`.

mysite/urls.py


from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
	url(r'^polls/', include('polls.urls')),
	url(r'^admin/', admin.site.urls),
]

The ʻinclude ()` on the 5th line seems to be used to refer to other URLconf files.

If you can do so, start the server again and check.

$ python manage.py runserver

Access http://127.0.0.1:8080/polls or http: // localhost: 8080 / polls with your browser. If "Hello, world. You're at the polls index." Described in polls / views.py is displayed, it is successful. Although not written in the tutorial, if you access http: // localhost: 8080 / admin here, the administrator login screen will be displayed. We'll talk about this in the next "About url ()" regex.

This is the end of Tutorial 1 for the time being. Thank you for your hard work.

About url ()

Since there was a description about ʻurl () used in ʻurls.py, I will write it as far as I can understand.

ʻUrl ()` has four arguments.

url(regex, view [,kwargs, name])

The last two (kwargs, name) are optional.

regex

Regular expression as the name suggests. Specify the URL pattern here. For mysite / urls.py, When it matches r'^ polls /' → Refer to the URLconf of polls When it matches r'^ admin /' → See ʻadmin.site.urls` And so on.

As a test, change ʻurl ()ofpolls / urls.py` as follows.

polls/urls.py


	:
url(r'^test', views.index, name='index'),
	:

Now when I save and start the server again and access http: // localhost: 8080 / polls, I get a 404 error. Next, try accessing it by adding a string that matches r'^ test' (such as test01) to the end of the previous address (example: http: // localhost: 8080 / polls). Then, as I did in the tutorial, the character string was displayed properly.

In short, the regex pattern determines the address.

view

If the regex of the first argument is matched, the function of this second argument is called. The HttpRequest object is given as the first argument to this function, and all the values obtained from regex are given after the second argument.

I understand that the HttpRequest object is passed, but I'm not sure about the second and subsequent arguments. Will the parameters passed by GET etc. be included ... I hope I can understand it as I go through the tutorial.

Recommended Posts

First Django development
First Django Challenge
[Memo] Django development environment
Django
Django environment development on Windows 10
django project development environment construction
Test Driven Development with Django Part 3
WEB application development using Django [Django startup]
Test Driven Development with Django Part 6
WEB application development using Django [Application addition]
[For beginners] Django -Development environment construction-
Test Driven Development with Django Part 2
Prepare your first Python development environment
Django Girls Tutorial Summary First Half
Django admin screen customization first step
Start Django for the first time
Test Driven Development with Django Part 1
Test Driven Development with Django Part 5
django update
Django note 4
WEB application development using Django [Model definition]
DORA development
Django memorandum
django search
Django installation
WEB application development using Django [Initial settings]
First Flask
[Remote development] Let's make it first !! (Practice 1)
Django Summary
Django test
First draft
Django # 2 (template)
Django Note 5
Django hands-on
Touch django
Django Summary
WEB application development using Django [Request processing]
Django Shoho
First python-review-
WEB application development using Django [Template addition]
Django + Docker
First gdb
Create your first app with Django startproject
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
Build Django + NGINX + PostgreSQL development environment with Docker
WEB application development using Django [Admin screen creation]
Until Django application creation by terminal (development environment)
[Python] Build a Django development environment with Docker
Build a Django development environment with Doker Toolbox
From 0 to Django development environment construction to basic operation
Pepper (first edition for general sale) development started