Home > Backend Development > Python Tutorial > Django learning to create web page examples

Django learning to create web page examples

零下一度
Release: 2017-07-23 11:25:48
Original
3982 people have browsed it

1. Select the default storage address of the project

The default address is C:\Users\Lee, which is the address to enter the cmd console. After creating the project, command django -admin startproject project_test creates a new project folder at the default address.

The project contains a manage.py file and the project_test folder

##2. Change the project storage address

The address I want to change is G:\Django. The cmd operation is to switch to the G drive first (the same goes for other drives) C:\Users\Lee> g: , and then G:\> appears. We then switch to the G:\Django folder and operate cd Django or cd G:\Django. The operation address is G:\Django

3. Create the first project hello at the new address

The command is django-admin startproject hello. If django-admin appears and it is not an internal or external command, then Try django-admin.py startproject hello

If there is no error, the creation is successful. The hello project folder has already appeared in the Django folder on the G drive

4. Create the first custom app——HelloWorld

First you have to enter the hello folder, command cd hello Now you have entered the hello file folder, command manage startapp HelloWorld. If no error occurs, the creation is successful. If an error occurs, try manage.py startapp HelloWorld

Please put the cmd console aside for the following 5-7 operations. There is no need to use the cmd console for the time being.

5. Add setting information

Add the customized HelloWorld to INSTALL_APPS## in G:\Django\hello\hello\settings.py

#, if the newly created app is not added to INSTALL_APPS, django will not be able to automatically find the template files (files under app-name/templates/) and static files (files in app-name/static/) )

#6. Define a URL function

Open the HelloWorld folder View function views.py, add the following code content, define an index function, pass in the request parameter, and send a web page request.

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello World!")
Copy after login

7. Associate the function with the access URL

Return to the previous level G:\ Django\hello Enter urls.py in the G:\Django\hello\hello folder again, add from HelloWorld import views to the head of the code, and add the following code to the urlpatterns list. As can be seen from the picture, django has already been installed by default. An admin URL has been added. We can access this admin URL later. r'^index/' in the url part is a regular expression, views.index is the associated address, name='Index' is the alias you gave this web page (not the title of the website), it is optional, even if it is The previous r'^index/' becomes r'^index2/', and index2 can also be accessed through this Index.

url(r'^index/',views.index,name='Index'),
Copy after login

Save the above file. So far we have completed the website construction. Let’s start the local server to browse the website we just built.

8. Use the cmd console command to start the local website server.

In the G:\Django\hello directory (because the manage command is required to run the server, this manage.py is only available in this project directory) enter manage runserver, and if nothing unexpected happens, the website server is set up It worked. But there is always a bug in everything, UnicodeDecodeError encoding error. For details, see "Django's Pitfalls (1)".

No error occurred, indicating that the operation was successful. The website access index address is http://127.0.0.1:8000/index/ or http://localhost:8000/ index/, and then you can see your first page. Do you remember the admin page? We visited http://127.0.0.1:8000/admin/ and found that the redirection 302 jumps to the login page. Because the super user has not been created yet, we cannot log in. At the same time, you can also find that the cmd console updates your operations synchronously, and the console has corresponding access records.

#Finally summarize the idea:

The new app is reported in the settings.py in the project name (hello) at the same level

—> The views.py in the new app folder is used to set the web content

—> The new app associates the URL in urls.py in the project name (hello) at the same level

—>Start the server

—>Access the corresponding website

The above is the detailed content of Django learning to create web page examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template