"Django" doesn't know why he wants to learn 01

Source: Internet
Author: User

1. Django installation, project creation, and server connectivity

System: Ubuntu 14.04.4

> cat/etc/issue   // View System version

Installing Django

> sudo pip install Django

Make sure you are in root mode, enter the directory where django-admin.py is located, and run the following command

> django-admin.py startproject MySite

Generate the MySite folder in the current directory, enter the folder (Oh, the project name can be arbitrarily taken, not necessarily mysite ... )

8000

Browser input:http://127.0.0.1:8000/. The following interface is displayed to connect the server successfully

2. Configuration of the urls.py file

Try to modify the contents of urls.py in the Manage folder.

The URL configuration is like the directory of the Web site that Django supports. Its essence is the URL pattern and the mapping table between the view functions to invoke for that URL pattern. That's how you tell Django to call that code for that URL and call that code for that URL.

When you execute django-admin.py startproject, the script automatically builds a copy of the URLconf (that is, the urls.py file) for you.

The full contents of each URL pattern are

Urlpatterns = patterns (prefix,    url (regular expression, view function, parameter dictionary, name),)

Modify the urls.py file to modify the Urlpatterns as follows

Urlpatterns = [   url (r'^admin/', admin.site.urls),       URL (r')  ^$'mysite.hello.helloworld'), #  i.e. the Hello_world function in mysite/hello.py ]

Create a hello.py file under the MySite folder

# -*-coding:utf-8-*- # hello.py  from Import HttpResponse def Hello_world (Request): # view function Hello_world    return HttpResponse ("  <p>Hello,World!</p>") # A view function must return a HttpResponse

Refresh the http://127.0.0.1:8001/and display the following

3. Database

Next, create the database, and under the first MySite folder, follow the instructions below

Python manage.py Migrate

4. Add App Features

Start by knowing what the app is.

Before the Starproject directive we have created the MySite project.

A project contains a number of Django apps and their configuration.

Technically, project's role is to provide configuration files, such as where to define database connection information, the list of installed apps, Template_dirs, and so on.

An app is a set of Django features, often including models and views, in the way Python's package structure exists.

For example, Django itself has some apps, such as a comment system and an automated management interface. One key point of the app is that they are easily ported to other project and reused by multiple project.

Create an App

Python manage.py Startapp blog    # Create blog This app

The blog folder is generated in MySite (not mysite/mysite), and the contents are as follows

blog    ├── migrations    |       __init__.py    ├── __init__.py    ├── admin.py    ├── models.py    ├── tests.py    └── views.py

After creating the application blog, find the settings.py file in MySite, add ' blog ' under Installed_apps and tell Django to use the blog app

Installed_apps = [    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Blog',#Add blog! ]

"=-= interrupt, because my Ubuntu has not been fully rooted, every time the input command is sudo su, edit the blog file and encounter permissions problems, you can enter the following command to get Super permissions, so you can edit all files"

> sudo sautilus

"Interrupted."

According to the official tutorial, first change the blog/models.py content to the following

 from __future__ Importunicode_literals fromDjango.dbImportModels fromDjango.utilsImportTimeZone#Create your models here.classPost (models. Model): #定义Post对象, models. Model indicates that the post is a Django models (? )
#定义,, title text created_date , published_date and author属性 author= Models. ForeignKey ('Auth. User') #models. ForeignKey represents a connection to another model title= Models. Charfield (max_length=200) Text=models. TextField () created_date=models. Datetimefield (Default=timezone.now) Published_date=models. Datetimefield (Blank=true, null=True)defPublish (self): Self.published_date=Timezone.now () self.save ( )def __str__(self):returnSelf.title

The above string for the time being forget =-=, slowly explain

5. Add a new model to the database

Create a data table for the model in the database, and run the following command in the MySite folder

> Python manage.py makemigrations Blog

Then

> Python manage.py Migrate Blog

Jiangzi post model is hot in our database

6. Background Management

Add code to blog/admin.py

 from Import Admin # Add the following code  from Import Postadmin.site.register (Post) # register the model with this line of code to make it visible on the page

Then open the website http://127.0.0.1:8001/admin, the login screen appears

"To log in, you need to create a super user who controls everything on the site . "

Continue to execute the following command

> Python manage.py createsuperuser

Then follow the prompts to enter the user name mailbox password blablabla~

After that, use the superuser you set up to login to the login interface.

Can post their own articles ~

"Countinued"

--------------------------------------------------------------------------------------------------------------- ----------------------------------------

Tutorials from the official (·?? •?)? : http://tutorial.djangogirls.org/

"Django" doesn't know why he wants to learn 01

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.