Django (a)

Source: Internet
Author: User

Tag: cal time localhost new input set DEX OCA app

Learned a few days of Django. According to the official tutorial learned 4 knots. Probably has a general understanding of Django.

There are differences between apps and project.

What's the difference between a project and an app? An app was a WEB application that does something–e.g., a Weblog system, a database of public records or a simple poll app . A project is a collection of the configuration and apps for a particular website. A project can contain multiple apps. An app can is in multiple projects.

Application refers to the application of a website. Project, however, refers to the collection of applications. It manages the operation of the entire website.

Then I built a project called MySite, according to the tutorial built a application called polls. is a simple voting system.

The default app also has apps such as user management.

The system configuration is in the setting of the MySite directory.

such as database configuration, middleware, templates and so on. and your new app.

Installed_app = [

' Polls.apps.PollsConfig ',
' Django.contrib.admin ',
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.messages ',
' Django.contrib.staticfiles ',


Some apps here are turned on by default. Like admin.

Then look at the new polls application.

Polls/    __init__.  PY    admin.  Apps.  Migrations__init__.  Models.  Tests.  Views.  PY                 

I'm talking about some of the files I used in my practice.

This is the file that created the database object.

Write the database model you need in this file.

Question(models.  Modelmodels.  Charfield(max_length=models.  Datetimefield(' date published ')          

The primary key is created by itself as a self-increment int.

Specific example:

Even the table names are generated for you.

How do I generate database data?

The command here is migrate.

What kind of data model is generated,

Look in this file.


You can see this before you execute the command.

Python sqlmigrate polls 0001

Final Execution command

Python Migrate

How does the program know polls this application?

In the polls directory under the to configure.

Migrate this command before it seems to have seen in Ror ...

It seems that these frameworks are actually similar.

And then the view.

The view is written mainly in the file under Application.

What if I use HTML?

Create a template/polls/xxx.html under polls

The program will follow this path to find.

Then enter localhost:8000/polls/in the browser.

How does the backstage know which function in is requested?

In the

The URL () function is enough.

[    URL(R ' ^$ 'views.  IndexName=' index '),]          

url(regex, view, kwargs=none, name=none)

The URL () function passes four parameters, the first being a regular expression, which is the URL request path.

Then the view, referencing the functions in

The latter two parameters are optional.

Kwargs is a supplement to the URL. As follows:

URL(R ' ^blog/(? P<year>[0-9]{4})/$ 'views.  Year_archive{' foo 'Bar '}),          

The requested URL is/blog/2005/, and the function calling isviews.year_archive(request, year=‘2005‘,foo=‘bar‘)

The last name can be used to define the name of the template.

Probably learned so much, and many of them are not very clear. We need to keep looking.

Django (a)

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: 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.