Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django No redirections

I have a flash file which calls a url like: http://test.com/savethis/123456/

I just want my view to save "123456" in the database and return nothing.

After saving the values what do I do? If I redirect it it changes the page and that's bad. I could render a page, but I don't want to. I just want it to end and not throw any errors.

like image 327
Fahim Akhter Avatar asked Jan 25 '10 09:01

Fahim Akhter


2 Answers

Make sure your URLConf points to your desired view function, and write something like:

from django.http import HttpResponse
from my_models import MyModel

def myview(request, number):
    my_model = MyModel(my_field = number)
    my_model.save()
    return HttpResponse()

The empty HttpResponse at the end there sends back a status code of 200 OK so that the browser or other server that connects to your endpoint knows the request was completed.

like image 133
Gabriel Hurley Avatar answered Oct 04 '22 21:10

Gabriel Hurley


It sounds like you're in a view function, which means someone's issued an HTTP request for something, which you have to respond to, so you can't just do nothing.

Either return an error code, or return an HttpResponse. You could just return an empty OK response (i.e. return HTTP response 200):

from django.http import HttpResponse

def myview(request):
    return HttpResponse()
like image 44
Dominic Rodger Avatar answered Oct 04 '22 22:10

Dominic Rodger