多个表单在一个页面上的视图分离处理
来源:互联网 发布:ubuntu 阿里源 gpg 编辑:程序博客网 时间:2024/06/05 21:08
V
from django.shortcuts import render, HttpResponse, redirectfrom firstapp.models import Article, Commentfrom django.template import Context, Templatefrom firstapp.form import CommentForm# Create your views here.def index(request): print(request) print('==='*30) print(dir(request)) print('==='*30) print(type(request)) queryset = request.GET.get('tag') if queryset: article_list = Aritcle.objects.filter(tag=queryset) else: article_list = Article.objects.all() context = {} context['article_list'] = article_list index_page = render(request, 'first_web_2.html', context) return index_page# def detail(request, page_num):# if request.method == 'GET':# form = CommentForm# if request.method == 'POST':# form = CommentForm(request.POST)# if form.is_valid():# name = form.cleaned_data['name']# comment = form.cleaned_data['comment']# a = Article.objects.get(id=page_num)# c = Comment(name=name, comment=comment, belong_to=a)# c.save()# return redirect(to='detail', page_num=page_num)# context = {}# #comment_list = Comment.objects.all()# article = Article.objects.get(id=page_num)# best_comment = Comment.objects.filter(best_comment=True, belong_to=article)# context['article'] = article# #context['comment_list'] = comment_list# context['form'] = form# if best_comment:# context['best_comment'] = best_comment[0]# return render(request, 'article_detail.html', context)###主视图def detail(request, page_num, error_form=None): context = {} form = CommentForm article = Article.objects.get(id=page_num) best_comment = Comment.objects.filter(best_comment=True, belong_to=article) if best_comment: context['best_comment'] = best_comment context['article'] = article if error_form is not None: context['form'] = error_form else: context['form'] = form return render(request, 'article_detail.html', context)###判断表格的视图def detail_comment(request, page_num): form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Article.objects.get(id=page_num) c = Comment(name=name, comment=comment, belong_to=a) c.save() else: return detail(request, page_num=page_num, error_form=form) return redirect(to='detail', page_num=page_num)
U
from django.conf.urls import urlfrom django.contrib import adminfrom firstapp.views import index,detail,detail_comment#从view层引入函数indexurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index', index, name='index'),#url(‘url地址’,函数名,查找方便名) url(r'^detail/(?P<page_num>\d+)$', detail, name='detail'), url(r'^detail/(?P<page_num>\d+)/comment$', detail_comment, name='comment'),]
T
<form class="ui error tiny form" action="{% url 'comment' article.id %}" method="post">#url/comment/article.id
<!DOCTYPE html>{% load staticfiles %}<html><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8"> <link href="https://fonts.googleapis.com/css?family=0swald|Raleway" rel="stylesheet"> <style type="text/css"> .ui.segment.container{ width:700px; } p { font-family:'Raleway', sans-serif; font-size:18px; } body { background: url({% static 'images/star_banner.jpg' %}); background-size:cover; background-repeat:no-repeat; background-attachment:fixed } </style></head><body> <div class="ui image"> <img src="" alt="" /> </div> <div class="ui segment padded container"> <h1 class="ui header" style="font-family:'Oswald',sans-serif;font-size: 40px"> {{ article.headline }} </h1> <p> {{ article.content }} </p> </div> <div class="ui segment container" style="width:700px;"> <h3 class="ui header" style="font-family: 'Oswald', sans-serif;">Comments</h3><!--标题Comment--> <div class="ui comments"><!--评论--> {% if best_comment %} <div class="ui mini red left ribbon label"> <i class="icon fire"></i> BEST </div> <div class="best comment"> <div class="avatar"> <img src="http://semantic-ui.com/images/avatar/small/matt.jpg " alt="" /> </div> <div class="content"> <a href="#" class="author">{{ best_comment.name }}</a> <div class="metadata"> <div class="date">2 days ago</div> </div> <p class="text" style="font-family: 'Raleway', sans-serif;"> {{ best_comment.comment }} </p> </div> </div> {% endif %} {% for comment in article.under_comments.all %} <div class="comment"> <div class="avatar"> <img src="http://semantic-ui.com/images/avatar/small/matt.jpg" alt="" /> </div> <div class="content"> <a href="#" class="author">{{ comment.name }}</a> <div class="metadata"> <div class="date">2 days ago</div> </div> <p class="text" style="font-family:'Raleway', sans-serif;"> {{ comment.comment }} </p> </div> </div> {% endfor %} </div> <div class="ui divider"></div><!--分割线评论--> <form class="ui error tiny form" action="{% url 'comment' article.id %}" method="post"> {% if form.errors %} <div class="ui error message"> {{ form.errors }} </div> {% for field in form %} <div class="{{ field.errors|yesno:'error, '}} field"><!--#依次判断表格中的field是否有error--> {{ field.label }} {{ field }} </div> {% endfor %} {% else %} {% for field in form %} <div class="field"> {{ field.label }} {{ field }} </div> {% endfor %} {% endif %} {% csrf_token %}<!--Django防跨站"{%csrf_token%}"标签--> <button type="submit" class="ui blue button">Click</button> </form> </div></body></html>
阅读全文
0 0
- 多个表单在一个页面上的视图分离处理
- 如何一个jsp页面处理多个form表单
- 多个django模型修改页面在一个页面上
- ASP中一个页面多个表单的提交
- Asp.net中一个页面多个表单的解决方案
- 将一个表单提交给多个页面的方法
- (三)vue 一个页面多个视图
- 当一个页面上有一百个表单
- 通过Jquery中的选择器选择相同的className统一处理表单(可以同时处理多个表单在一个网页中)
- 一个普通表单控制器(SimpleFornController)处理视图流转的例子
- 选项卡 一个页面支持多个 表现 结构分离
- springMVC处理表单重复提交,包含页面多个嵌套提交的情况,自定义token处理
- xpage一个视图事由多个表单组成
- 一个页面上显示ViewPager的多个item问题
- 一个页面的多个提交操作。。。各种条件处理
- 一个servlet处理来自多个不同页面的请求!
- 一个页面上出现多个listView
- 一个php页面处理两个form表单的方法
- 操作系统(6)
- 给初学者的 RxJava2.0 教程 (八)
- xcode卡死乱象
- faster rcnn训练自己地数据时遇到地问题
- React Native 在原生项目中集成之坑点总结
- 多个表单在一个页面上的视图分离处理
- iOS 核心动画
- hdu1729尼姆博弈
- 游戏策划游戏研发必读经典题文三
- OpenGL学习脚印: 向量和矩阵要点(math-vector and matrices)
- React Native for Android 发布独立的安装包
- hadoop完全分布式部署无法访问namenode的8020端口解决方案
- python爬虫(登录豆瓣并修改签名)
- python 利用浏览器 Cookie 模拟登录的用户访问知乎