チュートリアルからdjangoを使用してStartthedarkというアプリを作成していますが、エラーが発生しました。
できるだけ明確にエラーを説明しようと思います。できなかったらごめんなさい。
私のアプリには、説明を求めるテキスト ボックスが表示されます。説明を送信すると、同じページにリダイレクトされる代わりに、最新の説明を表示する別のページにリダイレクトされると思われます。
データが有効ではなく、同じページに戻るだけなので、問題は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'),
)
私のモデル.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 %}