0

I'm making a app called Startthedark using django from ground up tutorial and I bumped into an error.

I'll try to explain the error as clear as I can . Sorry If I can't.

My app display a a text box asking for a description and when I submit a description it's suppose to redirect me to another page that shows the latest description instead it redirect me to the same page..

enter image description here

Now I think the problem is with my views.py because the data can't valid so it just bring me back to the same page.Also it doesn't get submitted into the Event Models at admin page.

My 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)
     )

My 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',)

My 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'),
 )

My 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)

My 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 %}                
4
  • Can you show us what events/create.html looks like? Next is probably set there in the form. If it isn't, then it uses the url for 'ev_tonight' Commented Mar 2, 2013 at 5:40
  • Also, you are the second person I've seen today who has run into problems doing this specific tutorial. Django has an excellent beginners tutorial if this one is giving you trouble. docs.djangoproject.com/en/1.5/intro/tutorial01 Commented Mar 2, 2013 at 5:43
  • Yeah , I done that tutorial and I also posted my create.html .Thank for the help Ngentator Commented Mar 2, 2013 at 6:01
  • The if "next" in request.POST: may be for use later on in the tutorial. Currently I see nowhere that it is being used and since the check is handled properly in an if statement, you shouldn't need to worry about it for now. Commented Mar 2, 2013 at 6:11

2 Answers 2

2

Looks like you need to fix your template. The form tags appear to be wrong. You didn't properly close the method attribute, you used a single quote instead of double. That is why your form makes a GET request with the form data.

{% 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 %}
Sign up to request clarification or add additional context in comments.

6 Comments

good catch up, I didn't see that :) Maybe I need to be more concentrate
@donkeyboy72 So you have the csrf middleware enabled. That means that every form must include a csrf token or the view must be decorated with csrf_exempt. I will fix my answer to include the token.
Okay , I got this message "'User' object has no attribute 'message_set'"
I fixed the csrf . by placing {% csrf_token %}
That is caused by this line request.user.message_set.create(message='Your event was posted') You may have missed a step, from the error it looks like django is expecting you to have a Message model that contains a foreign key field to User with the fields related_name="message_set"
|
1
if form.is_valid():
     .....

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

5 Comments

yeah I didn't see your template codes. I just read your views. Good catch up by Ngenator
@catherine can you please send me some link for django-registration app customization I want to add some extra field and social login also
@masterofdestiny registration: github.com/nathanborror/django-registration
@catherine I already gone through that I am looking to customize django registration by adding extra fields
@masterofdestiny Sorry but I'm not fun of using third party app. I don't know customize django registration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.