我正在使用 django 从头开始制作一个名为 Startthedark 的应用程序,但我遇到了一个错误。
我会尽可能清楚地解释错误。对不起,如果我不能。
我的应用程序显示一个要求描述的文本框,当我提交描述时,它假设将我重定向到显示最新描述的另一个页面,而不是将我重定向到同一页面。
现在我认为问题出在我的views.py 上,因为数据无效,所以它只会让我回到同一页面。而且它不会提交到管理页面的事件模型中。
我的观点.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)
)
我的表格.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'),
)
我的模型.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 %}