"Translation" How to Tango with Django 1.5.4 Chapter III

Source: Internet
Author: User

Django Basics

3.1 Test your configuration

Test if your Python version is compatible with your Django version

3.2 New Django Project

In DOS, enter your new code folder, and then execute the following command to create a new project

c:\python27\scripts\django-admin.py startproject tango_with_django_project

After you've finished building your project, you'll tango_with_django_project see two other items in the folder

。。。。 Another folder tango_with_django_project of the same name

。。。。 A Python script manage.py

You'll see 4 files under Tango_with_django_project/tango_with_django_project.

__init__ an empty Python script that tells the Python compiler that this folder is a Python package

settings.py placing all the configuration of the Django project

urls.py Store the URL form of your project

wsgi.py is used to help run your development server, publish your project to the product environment

Note: Although two folders of the same name look a bit strange, the change happens when you create a standalone application.

In the project folder there is a file called manage.py. You will often use him during the development process.

Now use this command, provided you have entered the root directory of your project folder, enter

Python manage.py runserver

After executing this command, you will start Django's own lightweight server. You will see the information shown below in your terminal.

Now open the browser, enter Http://127.0.0.1:8000/, and you will see the following

You can use CTRL + C to stop the server in DOS

If you want to run the server on a different port, or allow other users to go to your site from another machine, you can use the following command:

Python manage.py runserver <your_machines_ip_address>:5555

Also, you need to replace the above 127.0.0.1 with your own IP address.

When setting up a port, you can not only use the default TCP80 port as the transport port for HTTP. 1024 of the following ports your system can use

If you don't use the default server to publish your app, because sometimes your coworkers might want to see your app too. You can enter http:<your_machines_ip_address>;port on his computer to see your app. Which, of course, depends on your network layout.

The note:djangp-admin.py and manage.py scripts provide many useful and time-saving features. Django-admin.py can create new projects and create new applications. You can see the exact meaning of each command in official Django documentation provides a detailed list and explanation of each possible command

3.3 New Django App

A Django project is a collection of configuration files and application folders that make up a Web application or a Web site. Create a standalone application to avoid repetitive wheel build-up

A Django application corresponds to a particular task. You need to create apps with different features for your site. For example, a website might include voting apps, registering apps, and special content-related apps. In another project, we may want to reuse these applications.

Now start creating a Rango app, under your project root (tango_with_django_project), run the following command

Python manage.py Startapp Rango

The above command creates a new folder called Rango, with 4 files in it.

Another __init__ file, just like the previous function

models.py, in order to store the data model of your application-that is, the relationship between your entity and your data

tests.py, the function of storing your application for testing

views.py processing client requests and responding

models.py and views.py, you'll often use the Django-compliant model-view-template

Before you create your own data model (models) and view (views), you need to tell your Django project the existence of this application, modify the settings.py file, find the Installed_apps tuple, add Rango to the end of the tuple, as

Run the server and see if that's right.

3.4 New View

After creating a new Rango app, let's create a new simple view. In our first view, just send some simple text to the client, regardless of models and template

Open Rango Apply the following views.py file, add the following code

From django.http import HttpResponse

def index (Request):

Return HttpResponse ("Rango says Helo world!")

The above few pieces of code have such a few knowledge points:

。。。 The first time we imported HttpResponse from the Django.http module

。。。 Each view is a separate function in views.py, and we have created only one view index

。。。 Each view requires at least one variable-a HttpRequest object, also in the Django.http module.

。。。 Each view must return a HttpResponse object.

Once the view is created, you simply provide a channel for the user to enter. In order for the user to see your view, you must map a URL to the view

3.5 Map URLs

Create a new urls.py file under the Rangp folder to allow URLs to be mapped to the corresponding view. Compare the following urls.py file

From Django.conf.urls import Patterns,url

From Rango Import views

Urlpatterns = Patterns ("',

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

)

This code imports rango/view.py, which can provide a more precise URL pointing

We're creating mappings in the Urlpatterns tuple. Each invocation only processes a unique mapping. In the above code, we used only one URL (), so we defined only one URL map at a time. The first argument we provide to the Django.conf.urls.url () function is the generic expression ^$, which matches an empty string. Any link that satisfies such a form can be accessed by Views.index (). This view passes a HttpRequest object as a parameter that contains information that the user has passed to the server. The name inside is an optional parameter

You may have discovered that there is already a urls.py file in your project configuration folder, why do you have to build another one? Technically, you can put all the URLs in your project application in this area. However, as your application grows, this is not a good experience. Standalone applications have a separate uel file, and finally you can concentrate them all in your main urls.py file.

That means we have to configure the urls.py file below the project to link the main project to our Rango application.

How do you do it? Open tango_with_django_project/tango_with_django_project/urls.py. Update the code as follows

Urlpatterns = Patterns ("',

#Examples:

#url (R ' ^$ ', ' tango_with_django_project.views.home ', name= ' home '),

#url (R ' ^tango_with_django_project/', include (' Tango_with_django_project.foo.urls ')),

#Uncomment the Admin/doc line below to enable admin documentation:

#url (R ' ^admin/doc/', include (' Django.contrib.admindocs.urls ')),

#Uncomment the next line to enable the admin:

#url (R ' ^admin/', include (Admin.site.urls)),

URL (r ' ^rango/', include (' Rango.urls ')),

)

The newly added map matches the URL string of the ^rango/. A match is forwarded to Rango.urls processing, which is implemented by Inlude (). Just like this link below. Look down one layer at a level. Finally find Rango the following urls.py, when the address of the input URL matches an empty string, call us to create the index () view

Restart the server to access http://127.0.0.1:8000/rango . If the operation is correct, the following effect will appear

3.6 Basic Workflow

3.6.1 to create a new application

Python django-admin.py startproject <name>

3.6.2 Create a new Django application

1.python manage.py Startap <appname>

2. Register your newly-created app in Installed_apps

3. In the urls.py below your project file, create a map to the app you just created

4. In your app's folder, create a new urls.py map view

5. In the view.py of your application folder, create a new request view and be able to return a HttpResponse object

3.7 Practice

3.7.1 Tips

"Translation" How to Tango with Django 1.5.4 Chapter III

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.