1

我正在做一个项目,我们需要添加一个表单来添加一个事件。它列出了姓名、日期、时间和地址。我让表单工作,但是当我添加基础时,表单没有显示在加载了基础的网页上。我认为这与我的 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>
4

2 回答 2

1

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

于 2012-12-11T02:49:15.923 回答
0

确保在 form.as_p 和大括号之间留有空间。像这样,{{ form.as_p }}

于 2012-12-10T22:01:36.607 回答