多个表单在一个页面上的视图分离处理

来源:互联网 发布: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