创建评论模型
from django.db import models# Create your models here.
class People(models.Model):name = models.CharField(null=True, blank=True,max_length=200)job = models.CharField(null=True, blank=True, max_length=200)def __str__(self):return self.nameclass Aritcle(models.Model):#新增headline = models.CharField(null=True, blank=True,max_length=500)#新增content = models.TextField(null=True, blank=True)#新增TAG_CHOICES = (('tech', 'Tech'),('life','Life'),)tag = models.CharField(null=True, blank=True, max_length=5, choices=TAG_CHOICES)def __str__(self):return self.headlineclass Comment(models.Model):#1name = models.CharField(null=True, blank=True, max_length=50)#1comment = models.TextField()#1def __str__(self):#1return selfment#1
迁移合并数据库
python manage.py makemigrations
python manage.py migrate
from django.shortcuts import render, HttpResponse, redirect
dels import Aritcle, Comment#2
plate import Context, Template
from firstapp.form import CommentForm# Create your views here.def index(request):print(request)print('==='*30)print(dir(request))print('==='*30)print(type(request))queryset = ('tag')if queryset:article_list = Aritcle.objects.filter(tag=queryset)else:article_list = Aritcle.objects.all()#变量article_list储存Article所有的文章context = {}#新建字典context['article_list'] = article_list #字典context中的article_list键(html中通过检索键提取文章内容)对应article_list变量的值index_page = render(request, 'first_web_2.html', context)return index_pagedef detail(request):hod == 'GET':form = CommentForm#HttpRequest对象request的method属性:表示提交请求使用的HTTP方法。它总是大写的。hod == 'POST':form = CommentForm(request.POST)if form.is_valid():name = form.cleaned_data['name']comment = form.cleaned_data['comment']c = Comment(name=name, comment=comment)c.save()return redirect(to='detail')context = {}comment_list = Comment.objects.all()context['comment_list'] = comment_listcontext['form'] = formreturn render(request, 'article_detail.html', context)
HttpRequest对象的属性
path属性
表示提交请求页面完整地址的字符串,不包括域名
如”/music/bands/the_beatles/”。
method属性
表示提交请求使用的HTTP方法。它总是大写的。例如:
hod == ‘GET’:
do_something()
hod == ‘POST’:
do_something_else()
GET属性
一个类字典对象,包含所有的HTTP的GET参数的信息。见 QueryDict文档。
POST属性
一个类字典对象&
本文发布于:2024-01-28 20:51:37,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170644630210205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |