Quickly create a Web page in the Django environment

Source: Internet
Author: User

Learning Goals:
10 minutes quickly create a page that queries the user list
The first step, in the SETTINGS.P inside the Installed_apps add the created app name, here with Hello
Installed_apps = [
' Django.contrib.admin ',
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.messages ',
' Django.contrib.staticfiles ',
' Hello ',
]

The second step is to define a business request handler function in views.py
Step three, define a template and introduce a static file
Create static and templates directories under the app, say CSS and js into static, table.hmtl into templates

Enable JS syntax in table.html
Variable with {{variable}} in JS, expression with {% Express%}
Fourth step, define the URL address inside the urls.py
In the project Hello_django directory there is a urls.py file, open can
Fifth step, start the service
The sixth step, the user data query and render to the page

After-school thinking:
1, the current template and static files are placed under the directory of the app, if we want to put the template directory and static files in the project (ie, project) root directory What to do?
Django defaults to look in the app directory, and if you move the template and static files to the root of the project, you can't find them, so here's how to adjust the project configuration file:
TEMPLATES = [
{
' Backend ': ' Django.template.backends.django.DjangoTemplates ',
' DIRS ': [],
' App_dirs ': True,
' OPTIONS ': {
' Context_processors ': [
' Django.template.context_processors.debug ',
' Django.template.context_processors.request ',
' Django.contrib.auth.context_processors.auth ',
' Django.contrib.messages.context_processors.messages ',
],
},
},
]

Switch
TEMPLATES = [
{
' Backend ': ' Django.template.backends.django.DjangoTemplates ',
' DIRS ': [' templates '],
' App_dirs ': True,
' OPTIONS ': {
' Context_processors ': [
' Django.template.context_processors.debug ',
' Django.template.context_processors.request ',
' Django.contrib.auth.context_processors.auth ',
' Django.contrib.messages.context_processors.messages ',
],
},
},
]

The configuration of the static file is adjusted to:
By:
Static_url = '/static/'
Switch
Static_url = '/static/'
Staticfiles_dirs = (
Os.path.join (Base_dir, ' static '),
)
Then restart the server and refresh the page
2, the current app URL is directly defined in the project directory under the urls.py, I would like to define the app urls.py can do?
Modify the urls.py under the project directory
From Django.conf.urls import URL
From Django.contrib Import admin
From Hello Import views
Urlpatterns = [
URL (r ' ^admin/', admin.site.urls),
URL (r ' ^hello/$ ', views.hello,name= ' hello ')
]

Switch
From Django.conf.urls import URL, include
From Django.contrib Import admin

Urlpatterns = [
URL (r ' ^admin/', admin.site.urls),
#url (R ' ^hello/$ ', views.hello,name= ' hello '),
URL (r ' ^ ', include (' Hello.urls '))
]

At this point, because there is no urls.py in the app Hello directory, you should create a separate urls.py
The corresponding function is then introduced in the urls.py.

From Django.conf.urls import URL
From Hello Import views

Urlpatterns = [
URL (r ' ^hello/$ ', Views.hello)
]

3, think of the template rendering process, the static file import label eventually become what?


Directory structure:
manage.py Command line tool footsteps
Hello_django (Project)
settings.py project configuration
urls.py URL Configuration
Configuration of the wsgi.py WSG

Quickly create a Web page in the Django environment

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.