Django: Form values to redirect to url in single step

How can I get values from a form, and redirect to a view called MyView like, MyView/formvalue1/formvalue2 in single step ? I have a solution but I think its not the right way to do it.

In detail:

I am trying to use Django-ratings, as suggested in tutorial

urlpatterns = patterns('',
    url(r'rate-my-post/(?P<object_id>\d+)/(?P<score>\d+)/', AddRatingFromModel(), {
        'app_label': 'blogs',
        'model': 'post',
        'field_name': 'rating',
    }),
)

I have a VoteForm view, which renders VoteForm.html. In VoteForm.html What should be the form action? If I make it /rate-my-post/ then I cannot regex since, the url goes like rate-my-post/?objectid=x&score=y, then it cannot be regex'ed it since Django does not see the url after ?.

My solution is: I created another view called junk, the form sends to it now, and the junk view gets the form values, and then junk redirects a HttpResponseRedirect to rate-my-post.

Answers


You should be using the same view (and the same url) to render the form as you use to process the form (have a look at this example in the django docs). On successful processing of your form use a reverse to redirect to the page you need.

Something like -

def vote_form(request):
    if request.method == 'POST': # If the form has been submitted...
        form = VoteForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect(reverse('my_view_name', 
                                                kwargs={'objectid':x, 'score': y}))
    else: # We're just rendering the html form
        form = VoteForm() # An unbound form

    return render(request, 'vote.html', {
        'form': form,
    })

Need Your Help

Error receiving POST request in Spring with Jackson JSON conversion

json spring post jackson

I'm new to both Jackson and Spring, so please excuse me if this is a dumb question. I've been working on a simple spring project which handles JSON requests. I have GET and DELETE working, but I'm

StreamReader.Read text out of order

c# streamreader networkstream

I'm trying to send the Base64 string of a screenshot to the server via NetworkStream and it appears I'm receiving the full string, problem is it's scrambled...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.