form_valid cannot be called in Django

558 views Asked by At

I followed an excellent guide (https://matthewdaly.co.uk/blog/2016/03/26/building-a-location-aware-web-app-with-geodjango/ ) and developed an application in Django 1.9 / Python 3 which works fine.

Now I'm trying to integrate the above application in a real django project in Django 1.8 / Python 2.7. I followed again the above guide modifying the code for Django 1.8 / python 2.7. I have an issue when I submit the data. The form_valid cannot be called. My models.py:

from django.contrib.gis.db import models

class Venue(models.Model):
    name = models.CharField(max_length=200)
    location = models.PointField()
    def __str__(self):
        return self.name

class Event(models.Model):
    name = models.CharField(max_length=200)
    datetime = models.DateTimeField()
    venue = models.ForeignKey(Venue)
    def __str__(self):
         return "%s - %s" % (self.name, self.venue.name)

My views.py:

class LookupView(FormView):
template_name = 'gigs/lookupresults.html'
form_class = LookupForm
success_url = '/'

def get(self, request):

    return render_to_response('gigs/lookup.html', RequestContext(request))

def form_valid(self, form):
    # Get data

    latitude = form.cleaned_data['latitude']
    longitude = form.cleaned_data['longitude']

    # Get today's date
    now = timezone.now()

    # Get next week's date
    next_week = now + timezone.timedelta(weeks=1)

    # Get Point
    location = Point(longitude, latitude, srid=4326)

    # Look up events
    events =  Event.objects.filter(datetime__gte=timezone.now()).filter(datetime__lte=next_week)

    # Render the template
    return render_to_response('gigs/lookupresults.html', {
        'events': events
    })

my urls.py:

urlpatterns = patterns(
'my_project.gigs.views',
url(r'^$',  LookupView.as_view(), name='lookup'),
)

my forms.py:

from django.forms import Form, FloatField

class LookupForm(Form):
    latitude = FloatField()
    longitute = FloatField()

my first template lookup.html

{% extends "gigs/gigs_base.html" %}
{% block content %}

<form role="form" action="/gigs/" method="post">{% csrf_token %}
    <div class="form-group">
        <label for="latitude">Latitude:</label>
        <input id="id_latitude" name="latitude" type="text" class="form-control"></input>
     </div>

    <div class="form-group">
        <label for="longitude">Longitude:</label>
        <input id="id_longitude" name="longitude" type="text" class="form-control"></input>
     </div>

    <input class="btn btn-primary" type="submit" value="Submit" />
    </form>
<script language="javascript" type="text/javascript">
    navigator.geolocation.getCurrentPosition(function (position) {
        var lat = document.getElementById('id_latitude');
        var lon = document.getElementById('id_longitude');
        lat.value = position.coords.latitude;
        lon.value = position.coords.longitude;
        });
 </script>
 {% endblock %}

my template after submit:

{% extends "gigs/gigs_base.html" %}
{% block content %}
<ul>
{% for event in events %}
     <li>{{ event.name }} - {{ event.venue.name }}</li>
{% endfor %}
</ul>
{% endblock %}

When I run localhost:8000/gigs the template is rendered fine but when I submit the data it displays nothing. Also when I execute the command:

Event.objects.filter(datetime__gte=timezone.now()).filter(datetime__lte=next_week)

in the django shell works fine. I cannot figure out why my first simple app that I developed following the above guide works fine while the new integrated app not. Any suggestions ?

1

There are 1 answers

0
raratiru On

In template lookup.html you have set:

<form role="form" action="/gigs/" method="post">

Try changing it to:

<form role="form" method="post">