Django表单将不会显示

时间:2012-12-10 21:58:17

标签: python html django forms post

我正在处理一个项目,我们需要添加一个表单来添加一个事件。它列出了名称,日期,时间和地址。我得到了表单,但是当我添加基础时,表单不会显示在加载了基础的网页上。我认为它与我的html文件有关。这是我的html文件。

{% extends "base.html" %}
{% block heading %}My Events{% endblock %}
{% block content3 %}
    </h1><b><font size="5">Save Event</b></h1></font>
    <form method="POST" action=".">{% csrf_token %}
        {{form.as_p}}
        <input type ="submit" value="Add Event"/>
    </form>

{% endblock %}

的观点:

def add_event(request):
    user = request.user
    events = user.event_set.all()
    if request.method == "POST":
        form = EventForm(request.POST)
        if form.is_valid():
            event = Event.objects.create(
            eventname = form.cleaned_data['eventname'],
            eventdate = form.cleaned_data['eventdate'],
            eventtime = form.cleaned_data['eventtime'],
            address = form.cleaned_data['address'],
            user = request.user
            )
            return HttpResponseRedirect('/')
    else:
        form = EventForm()
    variables = RequestContext(request, {
        'form': form
    })
    return render_to_response('add_event.html',variables)

HTML (base.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>DelMarVa Happenings | {% block title %}{% endblock %}</title>
  <link rel="stylesheet" href="/site_media/style.css" type="text/css" />
</head>
<body>
    <div id="wrapper">
        <div id="masthead">
            <div id="logo">
                <img src="/site_media/Delmarva.gif" width="100px" height="80px" />
            </div>
            <h1>DelMarVa Happenings</h1>
            <br />
            <h4>A listing of events in and around Delaware, Maryland, and Virginia</h4>
        </div>
        <div id="nav">
            {% if user.is_authenticated %}
                <h3>welcome, {{ user.username }}</h3>
            {% else %}
                <h3>welcome, guest</h3>
            {% endif %}
                <ul>
                    <li><a href="/">home<a/></li>
                {% if user.is_authenticated %}
                    <li><a href="/event/">add event</a></li>
                    <li><a href="/user/">my events</a></li>
                    <li><a href="/account/">my account</a></li>
                    <li><a href="/logout/">logout</a></li>
                {% else %}
                    <li><a href="/login/">login</a></li>
                    <li><a href="/register/">register</a></li>
                {% endif %}
                </ul>
        </div>
        <div id="ads">
            <img src="/site_media/ad.jpg" />
        </div>
        <div id="main">
            <h2>{% block head %}{% endblock %}</h2>
            {% block content %}{% endblock %}
        </div>
    </div>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

您的base.html没有{% block content3 %} - 在基础中重命名块名称,或者扩展模板以相互匹配。

答案 1 :(得分:0)

确保在form.as_p和大括号之间留出空格。因此,{{ form.as_p }}