Blog Application Project (detailed operations), blog details

Source: Internet
Author: User

Blog Application Project (detailed operations), blog details

1. cookie and session
Cookie can work independently
Cookie can also be used with session
Each browser has a cookie :{}
When the browser first accesses the server, the cookie is empty.
The server needs to ask the client to save some data cookies {"sessionID": "random string"}. The next request will contain some data you saved.
Each record in the django_session table is a user, similar to a large dictionary.
Session_key: stores the value corresponding to sessionID.
Session_data: {"": "", "": "",...} stores a group of key-value pairs.
When you access the service again, it will carry the sessionID


2. logon and Registration
-Logon (ajax)
-Verification code implementation is involved (you can store the verification code obtained in the session to facilitate judgment during verification. The browser will take the value you have stored in it every time you access it. You can use it as needed)
-Set the session after successful logon. the browser will carry a cookie each time the request comes in, and you will know who is in. The status is saved. (Using the auth module)

User = auth. authenticate (username = username, password = password) # verify the username and password if user: ret ["flag"] = Trueauth. login (request, user) # session request. session ["flag"] = True # If the authentication succeeds, log on. This login contains session operations and cookies...

If the user is not the current user, the user will not be allowed to access
-Registration (ajax + Form)
-Avatar implementation
For details, refer to the previous blog
3. System Homepage
1. design the system homepage (the system homepage is visible to users no matter they log on or not log on, so there is no need to verify it)
 -Navigation bar:
-Note the following:
-If a user logs in, the logon status is displayed. if the user has not logged in, the logon and registration () status is displayed on the navigation bar ()

Logon status

No Logon status

So we have to judge the specifics.

{% If request. user. is_authenticated %} <li class = "active"> <a href = "#"> <span class = "glyphicon-user"> </span> {request. user. username }}</a> </li> <li class = "active"> <a href = "/log_out/"> logout </a> </li> {% else %} <li class = "active"> <a href = "/login/"> logon </a> </li> <li class = "active"> 

  -Logout
  -Change Password
For example, we can use the auth module to log out, log on, and change the password.

-The specific display content can be divided into three parts:
-Menu series on the left
-Show article series
-Article Title:
-Author's profile picture:
-Who released the product, release time, and so on...
-There cannot be only one article, but there will be many articles. This requires additional paging.
-You can write other information on the right.

2. The following describes the specific procedures and knowledge points that need attention.
==========================================
Add two more tables to the left.
-Added a website category table and a website Article category table (refer to the system homepage of cnblogs.com in the blog Park)
-In a website category, there can be multiple types of articles (a one-to-many relationship is established)
-There are many related articles in the document category (a one-to-many relationship is established). Note that the correlated fields should be written on multiple parties.

Class SiteCategory (models. model): ''' website classification table ''' name = models. charField (max_length = 32, verbose_name = "category name") class Meta: verbose_name_plural = "website category table" def _ str _ (self): return self. nameclass SiteArticleCategory (models. model): ''' website document classification table ''' name = models. charField (max_length = 32, verbose_name = "category name") sitecategory = models. foreignKey (to = "SiteCategory", verbose_name = "") class Meta: verbose_name_plural = "文" def _ str _ (self): return self. name

 

==========================================
-After establishing the relationship, you can implement the left-side menu.
-In the left-side menu, you must note the jump path when you click the menu. Of course, some people will think of the method inherited by the template. This method can be implemented, but there is a more witty way. We can refer to the Redirect path of the blog Park, or continue to go to the index view,
But we need to match the path,

Url (R' ^ index/', views. index), url (R' ^ $ ', views. index), # The indexurl (R' ^ cate /(? P <site_article_category>. *)/', views. index) # name Group

 


Note:
[1]
Note that your index now has two paths, so we cannot directly upload site_article_category, we have to use a ** kwargs to accept it (because your url (R' ^ login/$ ', views. login) This path has no parameters,
If you directly upload site_article_category in this way, an error will be reported. We cannot tell whether there is a path or no path, so we use ** kwargs ), when there is a parameter, the famous Group transmits the parameter according to the keyword, so the passed value will be sent to kwargs in the dictionary form for receiving; when there is no parameter. Go to the index/path and directly display the index homepage.
[2]
When there is a conflict between the two paths, you must place less rules on those with more matching rules, otherwise it will overwrite the rules.
[3]
When rendering in a template, you need to check the data at the backend and then render the data at the front end.
==========================================
-Show article series
-Find out all the articles and render them on the page (as long as there is an article object, and use the. Method in the page during rendering)
It should be noted that you need to judge here. The details are as follows:

Print ("-----------", kwargs) # The dictionary kwargs = kwargs is received. get ("site_article_category") print ("======", kwargs) # obtain the value of site_article_category if kwargs: print ("xxxxxxxxxxxxxx ") article_obj = models. article. objects. filter (site_article_category _ name = kwargs) # article_obj prints an object. If you click on the left side, let the relevant article on the left side of reality else: article_obj = models. article. objects. all () # query all articles without clicking on the left

-Click the Avatar to jump

<Div class = "avatar col-md-2"> <a href = "{% url 'aaa' article. user. username %}"> <! -- Jump path --> here  </a> <! -- Image path --> </div>

Note:
[1]: Jump path

<A href = "{% url 'aaa' article. user. username %}"> <! -- Jump path --> here reverse resolution is used (matching the path by alias), because the url currently has parameters, so the parameter url (R' ^ (? P <username>. *)/$ ', views. homesite, name = 'aaa '),

[2]: The image path can be in two ways:

1. You can also specify/media/{article. user. avatar} 2. Directly {article. user. avatar. url} django will automatically find the corresponding position of the image.

-Paging

{% If article_obj.has_previous %} <li class = "previous"> <a href = "{request. path_info }}? Page = {article_obj.previus_page_number} "> previous page </a> </li> {% else %} <li class =" previous disabled "> <a href =" # "> previous Page </a> </li> {% endif %}

Note:
[1]: Make sure to match the jump path, otherwise it will be messy.

4. Personal Homepage
When designing a personal homepage, you can follow the design of the blog garden page. Of course, each person's personal homepage is different, but this is also quite simple. Next we will talk about
My personal homepage is still divided into three parts
-Navigation bar:
-Left side:
-Personal Information
-My tags
-Random Classification
-Date Archiving
-Right side: displays the article
-Knowledge points involved
1. template inheritance
It is used when you click the title of an article, because when you click the title of an article, you need to display the specific content of the article.
At this time, we need to take another route to handle the operations of specific article content.

Title #}< div class = "title"> 

Url: Route distribution is used here...

 url(r'^(?P<username>.*)/articles/(?P<article_id>\d+)/$', views.article_detail),

Note: When you enter the document content data in admin, If you paste the document you just want to find, there is no style to paste it. We need to paste the html source code into it.
When the source code is pasted in, it will all be tags, not the content of the article. This is because of the security mechanism. The tag is a string and we have to tell the browser.
My code is safe so that Chinese characters are displayed.
Solution: safe <p >{{ article_obj.article_detail.content | safe }}</p>
2. ORM Query
1. query operations

# Query all articles under the current user current_user = models. userInfo. objects. filter (username = username ). first () models. article. objects. filter (user = current_user) # view the current user's personal blog. (The relationship between a personal blog and a user is one-to-one. A user has a personal blog) current_user.blog # query all types and number of articles under the current user models. classfication. objects. filter (blog = current_blog ). annotate (count = Count ("article _ title ")). values_list ("title", "count") # query all the tags of the current user and the number of articles models. tag. objects. all (). filter (blog = current_blog ). annotate (count = Count ("article _ id ")). values_list ("name", "count") # query all the year, month, and date of the current user and the number of articles models. article. objects. all (). filter (user = current_user ). extra (select = {"filter_create_date": "strftime ('% Y/% m', create_time )"}). values_list ("filter_create_date "). annotate (Count ("title "))

Involved:
Object-based Query

Grouping function annotate Based on Double underscores

Understanding group:

Query Date: extra Filter

3. Label classification and casual classification on the left side, and date archiving. When you click the button, refer to the Redirect path of the blog Garden. The specific operations are similar to those on the system homepage and do not require template inheritance, you do not need to jump to other places, so that you can still jump to the current page
We can match the jump path.
Url (R' ^ (? P <username> .*)/(? P <condition> category | tag | data )/(? P <para>. *)/$ ', views. homesite ),

In HTML:

Date archiving, <p> <a href = "/blog/{current_user.username}/data/{data.0 }}/" >{{ data.0 }}( {data.1 }}) </a> </p> casual classification <p> <a href = "/blog/{current_user.username}/category/{category.0}/" >{{ category.0 }}( {category.1 }}) </a> </p> label classification <p> <a href = "/blog/{current_user.username}/tag/{tag.0}/" >{{ tag.0 }}( {tag.1 }}) </a> </p>

4. Everyone has a set of default skins.
1. Create a skin-saving folder named user_home_style under static.
A.css: Set a style
B .css: Set B Style
....
2. Delete the styles you don't want to use in homesite.
3. Modify theme in the database and save it as a.css.
B .css
....
4. Import: At link time, {current_user.blog.theme }}

<Link rel = "stylesheet" href = "/static/user_home_style/{current_user.blog.theme}"> # Find the style corresponding to each user.

5. Personal Homepage article details and comments

Knowledge points involved:
-Ajax
-F Query

Thumb ups: Implemented Using ajax:
Send data. To like that article, you need to send an article_id.
You also need to know the id of the current user. We don't need to use ajax to send this information. We can find it at the backend and use request. user. nid.
Receive data at the backend
Create a like example first
Models. Article_poll.objects.create (user_id = user_id, article_id = article_id)
Then click the number of likes + 1
Models. Article. objects. filter (id = article_id). update (poll_count = F ("poll_count") + 1)
Then return to the front-end, rendering at the front-end
Rendering time
1. Add 1 to the number on the bar page and the thumb ups are displayed successfully.
2. If you have already clicked the like button, you are prompted that you cannot click the like button again,
3. If a user logs on, click like. If no logon prompt is displayed, log on first. log on to a hyperlink to go To the login page.
6. url: path slash
When/is added, it is spliced from the root directory.
When no/is added, it is spliced from the current path.
7. view the current user

Current_user = models. userInfo. objects. filter (username = username ). first () print ("current_user ==========", current_user, type (current_user) print ("request. user. username = ", request. user. username, type (request. user. username) print the result current_user ========== haiyan <class 'app01. models. userInfo '> # Object request. user. username ===== haiyan <class 'str'> # string, which can be rendered on the page

 


 

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.