开发者

Django No redirections

I hav开发者_运维百科e 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.


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.


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜