Django表单无法验证

时间:2013-03-02 04:29:02

标签: django

我正在制作一个名为Startthedark的应用程序,使用django从头开始教程,我遇到了错误。

我会尽可能清楚地解释错误。对不起如果我不能。

我的应用程序显示一个文本框,询问说明,当我提交说明时,我想将我重定向到另一个显示最新说明的页面,而不是将其重定向到同一页面。

enter image description here

现在我认为问题出在我的views.py上,因为数据无效,所以它只会让我回到同一页面。而且它不会被提交到管理页面的事件模型中。

我的views.py

 from events.models import Event
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from events.forms import EventForm
 from django.core.urlresolvers import reverse
 from django.http import HttpResponseRedirect


 def tonight(request):
     events = Event.objects.filter(latest=True)
     return render_to_response('events/tonight.html',{'events':events},context_instance = RequestContext(request))


 def create(request):
     form = EventForm(request.POST or None)
     if form.is_valid():
         event = form.save(commit=False) 
         event.creator = request.user
         guessed_date = None
         event.start_date = guessed_date
         event.save()
         request.user.message_set.create(message='Your event was posted')
         if 'next' in request.POST:
             next = request.POST['next']
         else:
             next = reverse('ev_tonight')
         return HttpResponRedirect(next)
     return render_to_response(
         'events/create.html',
         {'form':form},
         context_instance = RequestContext(request)
     )

我的forms.py

 from django import forms
 from events.models import Event
 class EventForm(forms.ModelForm):
     description = forms.CharField(max_length=340,widget=forms.Textarea)

     class Meta:
         model = Event
         fields = ('description',)

我的URL.conf

 from django.conf.urls.defaults import *
 from django.contrib import admin
 from events import views
 urlpatterns = patterns('events.views',
         url(r'^tonight/$','tonight',name='ev_tonight'),
         url(r'^create/$','create',name='ev_create'),
 )

我的models.py

  from django.db import models
 from datetime import datetime, timedelta
 from django.contrib.auth.models import User
 from django.db.models.query import QuerySet


 class Event(models.Model):
     description = models.TextField()
     creation_date = models.DateTimeField(default = datetime.now)
     start_date = models.DateTimeField(null=True, blank= True)
     creator = models.ForeignKey(User , related_name = 'event_creator_set')
     attendees = models.ManyToManyField(User , through = "Attendance")
     latest = models.BooleanField(default=True)
     def __unicode__(self):
         return self.description
     def save(self, **kwargs):
         now = datetime.now()
         start = datetime.min.replace(year = now.year , month=now.month,day=now.day)
         end = (start + timedelta(days=1)) - timedelta.resolution
         Event.objects.filter(latest=True,      creator=self.creator).filter(creation_date__range=(start,end)).update(latest=False)
         super(Event,self).save(**kwargs)

 class Attendance(models.Model):
     user = models.ForeignKey(User)
     event = models.ForeignKey(Event)
     registration_date = models.DateTimeField(default=datetime.now)

     def __unicode__(self):
         return "%s is attending %s" %(self.user.username,self.event)

我的create.html

  {% extends "tin.html" %}
  {% block title %}Create - {{ block.super }}{% endblock %}
  {% block main_content %}
          <form method = "POST' action"">
              <ul>
              {{ form.as_ul }}
          </ul>
          <input type = "submit" value="Create Event" />
      </form>
  {% endblock %}                

2 个答案:

答案 0 :(得分:1)

if form.is_valid():
     .....

     return HttpResponseRedirect(reverse('events:ev_tonight'))

答案 1 :(得分:1)

您似乎需要修复模板。表单标签似乎是错误的。您没有正确关闭方法属性,而是使用单引号而不是double。这就是您的表单使用表单数据发出GET请求的原因。

{% extends "tin.html" %}
{% block title %}Create - {{ block.super }}{% endblock %}
{% block main_content %}
    <form method="POST" action"">
        {% csrf_token %}
        <ul>
            {{ form.as_ul }}
        </ul>
        <input type = "submit" value="Create Event" />
    </form>
{% endblock %}