Today, we come together to add a detailed page under the simple comment function, but also a simple to I am embarrassed to say that the east .... Of course, the lofty high-rise floor, we start from the simple, and then slowly add chant.
or follow the old steps of the past, Models>views>urls>templates.
because we're going to have one more comment function, we need to create a new table to hold the comment data. The following things have been added to the models:
class blogcomment (models. Model): user_name = models. Charfield (' Reviewer name ', max_length=100) user_email = models. Emailfield (' reviewer Mail ', max_length=255) body = models. TextField (' comment content ') created_time = models. Datetimefield (' Comment release time ', auto_now_add=true) article = models. ForeignKey (' article ', verbose_name= ' review of articles ', on_delete=models. CASCADE) def __unicode__ (self): RETURN SELF.BODY[:20]
We save some simple data, the first few are nothing special, article is a foreign key, associated with one of our article. So each comment belongs to an article, an article can have a lot of comments.
Next, we revise our views. No, no, we should add a new forms form to tell Django what form items we need to display in the display page and how to display them. Kids who don't know the Django form can go to the official website to see a list of Django forms overview
Anyway I can not understand is a statement ghost, do not understand to follow the wind wind teacher together to do ~ (yuck!) Brazen! Cough, we continue, reasonable words, if you add Markdown Editor in the background, you have already created a new forms.py in the myblog/article/directory, there is a views.py directory, if not, then you will be a new one. The code is as follows
from models import article, blogcommentfrom django import formsclass Blogcommentform (forms. Modelform): class meta: model = blogcomment #关联一个model fields = [' user_name ', ' user_email ', ' body '] #这里是指我们表单要渲染的字段 widgets = { #这里定义我们如何渲染, # For example, user_name rendered HTML component as follows: # <input type= "text" class= "Form-control" placeholder= "Username" aria-describedby= "Sizing-addon1" > ' user_name ': forms. TextInput (Attrs={ ' class ': ' Form-control ', ' placeholder ': "Please enter your nickname", ' Aria-describedby ': "Sizing-addon1", }), ' User_email ': forms. TextInput (attrs={ ' class ': ' Form-control ', ' placeholder ': "Please enter your email", ' Aria-describedby ': "Sizing-addon1", }), ' body ': forms. Textarea (attrs={' placeholder ': "I'm going to comment on two sentences ~"}) }
That meta is called the META option. It is used to define the behavior characteristics of some Django model classes.
We continue to use the class-based view, (can not understand the comments, you may first look at the following extracted skeleton comments)
Def post_comment (request, article_id): #从urls处接受到request请求 and receives a parameter article_id if request.method == ' POST ': #判断这个请求是不是post请求, Make different treatment according to the result of judgment. form = blogcommentform (Request. Post) #如果是, we get the form data for Post if form.is_valid (): # after we get the data, we have to see if the data is worth it, and if it's legal we save the data to the database comment_form = form.cleaned_data #如果有值我们就在在cleaned_data属性中找到合法的表单数据 user_name = comment_form[' User_name '] user_email = comment_form[' User_email '] body = comment_form[' body '] &nbSp; comment = blogcomment.objects.create ( #只是创建一个新的BlogComment user_name=user_name, user_email=user_email, body=body, article=article.objects.get (pk=article_id)) comment.save () return httpresponseredirect ('/detail/%s/' % article_id) after saving to the database we will redirect to the article detail page else: If it is not a POST request, our first access to comment URL is not a POST request Form = blOgcommentform () #因为是第一次访问, so we'll render the form used to get the user and get an empty form form return render ( request, ' article/comment.html ', {' form ': form}) #把form表单传递对comment. html
> Well, maybe you look a little complicated at first, but it's not complicated, so let's extract its skeleton and analyze it,
def post_comment (Request): #和以前一样, receives request requests from URL, if request.method == ' Post ': Determine if it is a POST request form = blogcommentform ( Request. POST) #如果是, we get the data from the form, if form.is_valid (): Of course we have to judge it and not be legal before we send it over. Legally we can save the data to the database ... else: #如果不是post请求, in doing other operations ~ ..
To understand slowly. Ben also just learned to write the blog ...
And then we have to implement the form page. Create a new comment.html file in the myblog/articel/templates/article/directory, the code is simple, as follows:
<form method= "post" > {% csrf_token%} <div class= "Form-group" > {{form}} </div> &L T;input type= "Submit" value= "Submission" ></form>
{% Csrf_token%} is for [cross-site request forgery protection] (http://python.usyiyi.cn/django_182/ref/csrf.html)
{{form}} is our form, it will be rendered according to the settings in form.py, but it is ugly and ugly, in order to realize the function, I! Endure! The
And the form does not provide a button, so we finally add a submit to the child
Reasonable words, basically already can write data to the database, next we must display the data to the article detail page,
So we're going to change views.py.
def article_detail (Request, article_id): Article = Article.objects.get (pk=article_id) comment_list = Blogcomment.obj Ects.filter (ARTICLE__ID=ARTICLE_ID) return render (Request, ' article/detail.html ', {' article ': article, ' Comment_list ': Comment_list})
... {% for comment in comment_list %} <div class= "Panel panel-info" > <div class= "Panel-heading" > Our simple comment function adds up to this. However, there is a small problem, inexplicably add a comment page is not stupid da, we can try to add the comments page to the detail.html page below ~ Refueling! Finally, if you want to be able to edit the content of comments in the background, Just register the blogcomment in the admin.py.
This article is from the "mechanism of small Wind" blog, please be sure to keep this source http://xiaofengfeng.blog.51cto.com/8193303/1885772
Blog commenting features