Django Learning to make a Web page instance

Source: Internet
Author: User
1. Select the address where the project is stored by default

The default address is C:\Users\Lee, which is the address of the CMD console, and after the project is created, the command django-admin startproject project_test default address creates a new project folder.

Project contains a manage.py file and a project_test folder

2. Replace the project storage address

I want to change the address is G:\Django, cmd operation is to switch to the G-disk (the same as other disks) C:\users\lee>g:, and then appeared g:\>, we switch to the G:\Django folder, Operation CD Django or CD g:\ The Django operating address is G:\Django.

3. Create the first item in a new address hello

The command is django-admin startproject Hello, if django-admin appears not internal or external command, then try django-admin.py startproject Hello

If the error is created successfully, the G-Disk Django folder already appears in the Hello project folder

4. Create your first custom app--helloworld

First you want to enter the Hello folder, the command CD Hello now has entered the Hello folder, command manage Startapp HelloWorld, if the error is created successfully, if error try manage.py Startapp HelloWorld

The following 5-7 operations please put the CMD console on one side, the CMD console is not required for the time being.

5. Adding Settings information

Add the custom HelloWorld to the Install_apps in G:\Django\hello\hello\settings.py, and if the new app doesn't add to Install_apps, Django Cannot automatically find template files (files under app-name/templates/) and static files (files in app-name/static/) in the app

6. Defines a URL function

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

From django.http Import httpresponsedef Index (Request):    return HttpResponse ("Hello world!")

7. Associating a function with an access URL

Return to the previous level G:\Django\hello again into the urls.py in the G:\Django\hello\hello folder, add the From HelloWorld import views to the code header, Add the following code to the Urlpatterns list, as shown in fact Django has already added an admin URL by default, so we can access the admin URL. The R ' ^index/' in the URL section is a regular expression, Views.index is the associated address, name= ' index ' is the alias you gave the page (not the title of the site), optional, even if the front R ' ^index/' became R ' ^ Index2/', can also be accessed through this index to INDEX2.

URL (r ' ^index/', views.index,name= ' index '),

Save the above file, so far we have completed the Web site construction, the following start to start the local server to browse our newly built web site.

8. Use the CMD console command to start the local Web site server.

In G:\Django\hello this directory (because the server needs to run Manage command, only this project directory has this manage.py) input manage runserver, not surprisingly, the site server was built successfully. But there is always a bug,unicodedecodeerror coding error, see the "Django trampled Pit (i)" for details.

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/, then you can see your first page, Do you remember the admin page? We visited the next http://127.0.0.1:8000/admin/found redirect 302 jump to login page, because also Wood has created super User so can't log in. Also, you can find that the CMD console updates your operation synchronously, and the console has access records accordingly.

Finally summarize the idea:

New app settings.py in the same level project name (hello)

Views.py in the new app folder for Web content settings

New app URL Association in urls.py in the same level of project name (hello)

-Start server

Visit the corresponding website

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.