Writing my first Django app - 1

作者: 游文影月志 | 来源:发表于2020-07-25 18:05 被阅读0次

1. Install Django

pip install Django

2. Create a project

From the command line, cd into a directory where I want to store my code, then run the following command:

django-admin startproject mysite

This will create a mysite directory in my current directory.

  • The outer mysite/: The root directory which is a container for my project.
  • manage.py: A command-line utility that let me interact with this Django project in various ways.
  • The inner mysite/: This directory is the actual Python package for my project.
  • mysite/init.py: An empty file that tells Python that this directory should be considered a Python package.
  • mysite/settings.py: Settings/configuration for this Django project.
  • mysite/urls.py: The URL declarations for this Django project; a TOC of my Django-powered site.
  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve my project.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve my project.

3. The development server

cd into the outer mysite directory, and run the following commands:

py manage.py runserver

Visit http://127.0.0.1:8000/ with the web browser.

4. Create the Polls app

py manage.py startapp polls

5. Write the first view

polls/views.py

from django.http import HttpResponse


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

To call the view, I need to map it to a URL - and for this I need a URLconf.
To create a URLconf , create a file called urls.py in the polls directory.

polls/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

The next step is to point the root URLconf at the polls.urls module.

mysite/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('polls/', include('polls.urls')),
]

Visit http://127.0.0.1:8000/polls with the web browser.

The path() function is passed 4 arguments, 2 required: route and view, and 2 optional: kwargs, and name.

5.1 The path()argument: route

route is a string that contains a URL pattern. When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.

Patterns don't search GET and POST parameters, or the domain name.

5.2 The path()argument: view

When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments.

5.3 The path()argument: kwargs

Arbitrary keyword arguments can be passed in a dictionary to the target view.

5.4 The path()argument: name

Naming the URL makes it easier to refer to it from elsewhere in Django, especially from within templates.

相关文章

网友评论

    本文标题:Writing my first Django app - 1

    本文链接:https://www.haomeiwen.com/subject/ezjmlktx.html