Django - 形式和视图混乱

时间:2014-03-03 14:35:26

标签: html django django-forms django-templates django-views

我目前有一个简单的网站,提交的表单非常简单,但由于组织我试图实现,我已经有了一个冗长,重复和丑陋的代码。 基本上我有两种观点:

def base(request):

if request.method == "POST":
    if "seetag" in request.POST:
        seetagform = SeeTagForm(request.POST)
        if seetagform.is_valid():
            cd = seetagform.cleaned_data
            p = Tag.objects.get(name=cd["name"])
            return HttpResponseRedirect(reverse("main:see",args=(p.id,)))
        else:
            createform = CreateForm()
            return render(request,"tags/base.html",{"seetagform":seetagform,
                                                    "createform":createform})
    if "newevent" in request.POST:
        createform = CreateForm(request.POST)
        if createform.is_valid():
            cd = createform.cleaned_data
            print cd["day"]
            return HttpResponseRedirect(reverse("main:base"))
        else:
            seetagform = SeeTagForm()
            return render(request,"tags/base.html",{"seetagform":seetagform,
                                                    "createform":createform})   
else:
    seetagform = SeeTagForm()
    createform = CreateForm()                           
    return render(request,"tags/base.html",{"seetagform":seetagform,
                                            "createform":createform})

def see(request,tag_id):

if request.method == "POST":
    if "seetag" in request.POST:
        seetagform = SeeTagForm(request.POST)
        if seetagform.is_valid():
            cd = seetagform.cleaned_data
            p = Tag.objects.get(name=cd["name"])
            events = p.events.all()
            return HttpResponseRedirect(reverse("main:see",args=(p.id,)))
        else:
            createform = CreateForm()
            p = Tag.objects.get(pk=tag_id)
            events = p.events.all()
            return render(request,"tags/see.html",{"p":p,
                                                    "events":events,
                                                    "seetagform":seetagform,
                                                    "createform":createform})
    if "newevent" in request.POST:
        createform = CreateForm(request.POST)
        if createform.is_valid():
            cd = createform.cleaned_data
            print cd["day"]
            return HttpResponseRedirect(reverse("main:see",args=(tag_id,)))
        else:
            seetagform = SeeTagForm()
            return render(request,"tags/see.html",{"p":p,
                                                    "events":events,
                                                    "seetagform":seetagform,
                                                    "createform":createform})

else:
    seetagform = SeeTagForm()
    createform = CreateForm()
    p = Tag.objects.get(pk=tag_id)
    events = p.events.all()                             
    return render(request,"tags/see.html",{"p":p,
                                            "events":events,
                                            "seetagform":seetagform,
                                            "createform":createform})   

其中SeeTagForm只是一个form.CharField和CreateForm只是一个form.IntegerField。你可以注意到它们几乎相同。 这两个模板是:

base.html文件:

<head>
<div class="back">
    <h1> Flux </h1>
    <hr>
</div>
</head>

<body>
<p>
    <form action="{% url 'main:base' %}" method="POST">
        {%for error in seetagform.name.errors %}
        <p> {{ error }} </p>
        {% endfor %}
        {% csrf_token %}
        <button class="btn" type="submit" name="seetag">See</button>
        {{ seetagform.name }}
    </form>
</p>


<p>
    <form action="{% url 'main:base' %}" method="POST">
        {%for error in createform.day.errors %}
        <p> {{ error }} </p>
        {% endfor %}
        {% csrf_token %}
        <button class="btn" type="submit" name="newevent">Create</button>
        {{ createform.day }}
    </form>
</p>

</body>         

see.html:

<head>
<div class="back">
    <h1> Flux </h1>
    <hr>
</div>
</head>

<body>
<p>
    <form action="{% url 'main:see' p.id%}" method="POST">
        {%for error in seetagform.name.errors %}
        <p> {{ error }} </p>
        {% endfor %}
        {% csrf_token %}
        <button class="btn" type="submit" name="seetag">See</button>
        {{ seetagform.name }}
    </form>
</p>

{% if events %}
    <ul>
        {% for ev in events %}
            <li> {{ ev.start|date:'H:i d/m/Y'}} </li>
        {% endfor %}
    </ul>
{% endif %} 

<p>
    <form action="{% url 'main:see' p.id%}" method="POST">
        {%for error in createform.day.errors %}
        <p> {{ error }} </p>
        {% endfor %}
        {% csrf_token %}
        <button class="btn" type="submit" name="newevent">Create</button>
        {{ createform.day }}
    </form>
</p>

</body>

我想要实现的是以下内容。 当您进入网站时,它会显示base.html。现在,如果您有效地提交seetagform,将使用see view(和template)显示具有该名称的Tag内的对象。

之所以如此混乱,是因为我希望在所有页面上保持相同的html提交布局,但是,如上所述,如果我要显示对象,它将使用see视图。当我必须在两个视图中编写表单提交的所有可能结果时,显然会出现混乱(在基本视图中,如果提交了无效表单,则在视图中显示并显示错误,而不进入查看视图;在看到视图,如果提交了一个无效的表单,它将保留在see视图中,显示错误,但必须在see模板中继续显示它在提交之前显示的内容;你得到了一团糟!)

你对我应该如何构建这个有什么建议吗?基本上我想让html的某些部分始终相同,但根据输入调用不同的视图。我应该使用jQuery来处理某种“通用”表单提交吗?

对于长代码表示歉心,并提前致谢,

0 个答案:

没有答案