Bibliography: Django Web Development Guide, Django official documentation
Environment Windows 8 x64, python2.7.9 x32,django1.8.1
Because the Django version of the book seems to be older, so here I refer to the official online documentation of the relevant parts of the book, the following is recorded in the book Code in the new version of the implementation.
1. Installation and other minor
2. Create a new project
The newly created project structure is as follows:
3. Database connection
4. Start the test server for the Django development environment:
Enter Http://127.0.0.1:8000/in the browser address bar to test for success
After the Runserver command, you can specify the port number and IP.
Such as:
manage.py runserver 80800.0.0.0:8080
This server does not start successfully when there is an error in the project, so you can view the exception problem.
Successful words:
5. Create the App module:
6. Create the model used in the app:
The model needed to create the project requires that the inheriting class be written in the models.py of the above file:
7. Add the app module to your project:
8. Connect the data table for the newly created module:
9. Refreshing the database
10. Create an Administrator account:
This future is the verification link needed to log into the project homepage.
At this point, start the test server and type the address in the browser http://127.0.0.1:8000/admin/
can see:
Enter the user name and password you just set to log in to the console:
The App/blog module I created is not shown here, and I need to introduce and register the module in admin.py
11. Register the module:
Then go to the interface just now:
Successfully imported, now you can enter the blog under the module to edit, click on the right side of the add to enter the new blog post interface:
Just fill in the required content, and then click Save to complete the following:
The successfully created interface displays a list of the currently established post:
The problem here is that the name is the default, it feels ugly, in order to customize the display format, you need to do the next step:
12. Custom Display Entries:
In order to display names in the added entries using the model custom rules, the simplest way is to define the __str__ method under the model class to return a string, such as:
Effect:
Of course, you can also customize the entry to show how many parameters:
Refresh the test server and refresh the page just now:
Add such a sentence to the same document:
This defines the entry arrangement for the interface that created the post:
Add a title for the entry: (Here the item changes to the polls project for the document sample)
Effect:
Show/hide, show only entries:
Effect:
Other personalization settings See official documentation
13. Association entries (associating question to choice)
Display effect:
14. Define the HTML template that displays the model
The following items go back to the blog project, in order to display the information of the blog to a page, you need to customize an HTML template to support the display, create a templates directory under the blog, and add an arbitrary named HTML file:
The new HTML file is as follows:
Index.html
1 {% for post in posts%}2 <H2>{{Post.title}}</H2>3 <P>{{Post.timestamp}}</P>4 <P>{{Post.body}}</P>5{% ENDFOR%}
The code that is sandwiched between {percent-percent} and {{}} is executed instead of HTML tag.
15. Define the view variables required by the template
See the above template HTML in the appearance of a significant external incoming volume named posts, we need to define a view to get and pass to this value, the standard views are defined as follows:
Of course the first function to get the object is different, can be selected according to the document as required.
16. Define URL Orientation
According to the MVC pattern principle, there should be a control layer here, I think the control layer here is the URL orientation problem, to find the page just created based on the URL
Need to define in two places, a definition of how to find a blog, a blog in the definition of how to find the sub-views of the blog
The first is the project's URLs:
This indicates that the URL starting with blog/needs to go to blog.urls below to find a match, which requires a second definition
Create the urls.py file in the blog directory:
The contents are as follows:
This completes the definition, accesses the http://127.0.0.1:8000/blog/this address, appears is follows the definition template to display the webpage:
By loading the CSS we can also define his style:
Effect:
Background picture is Bing's most recent wallpaper
This is the result of last night and today.
Done.
First knowledge of Django