Home Django doesn't redirect to same or other page after successful HTTP 302 and 200
Reply: 0

Django doesn't redirect to same or other page after successful HTTP 302 and 200

user2931 Published in April 25, 2018, 10:53 am

I've been scratching my head all day long but I don't seem to get it working. My redirects work just fine everywhere else in the app, but don't work when I'm using the forms.py with ModelForm class. So what happens when I hit 'Save' on the form is Django actually does the save successfully and then reports couple of lines in the Debug console as follows, but the browser just stays stalled on the same page without even attempting to go to another page (I've tried redirecting to various views with and without reverse, using HttpResponseRedirect, using redirect, using render_to_response, etc. - just about anything I could find, but Django behaves the same way all the time (see below). It's not a browser caching issue since I've tried redirecting to even the external pages such as google for example, and every time all I'm getting is this:

[05/Dec/2017 17:00:41] "GET /music/release/test/edit HTTP/1.1" 200 3310  
[05/Dec/2017 17:00:46] "POST /music/release/test/edit HTTP/1.1" 302 0  
[05/Dec/2017 17:00:46] "GET /music/releases/ HTTP/1.1" 200 13616  

Can anyone please help as I'm really stuck, since all the other redirects are working just fine, it's just the form one that fails for whatever reason. Thx in advance!

Here's the template editrelease.html:

<form method="POST" class="release-form">{% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="save btn btn-default">Save</button>

This is the forms.py:

from django import forms
from .models import Release

class ReleaseForm(forms.ModelForm):
    class Meta:
        model = Release
        fields = ('rls', 'artist', 'album', 'year', 'genre')

The model looks like this:

class Release(models.Model):
    rls = models.CharField(max_length=256)
    artist = models.CharField(max_length=128)
    album = models.CharField(max_length=128)
    year = models.IntegerField()
    genre = models.CharField(max_length=32)
    def __str__(self):
        return self.rls

The method/view is as follows:

def editrelease(request, release):
    releaseform = get_object_or_404(Release, rls=release)
    if request.method == "POST":
        form = ReleaseForm(request.POST, instance=releaseform)
        if form.is_valid():
            releaseform = form.save(commit=False)
            releaseform.rls = release
            return redirect('/music/releases/')
        form = ReleaseForm(instance=releaseform)
    return render(request, 'music/editrelease.html', {'form':form})

My URL patterns have the following patterns included:

url(r'^releases/$', views.releases, name='releases'),
url(r'^release/(?P<release>(.)+)/edit$', views.editrelease, name='editrelease'),
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302296 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO