[Django]-View function is not being called after submitting form in django

2πŸ‘

βœ…

You can not attach two views to the same URL. The {% url ... %} template tag, only generates a URL for that path. But if there is a β€œurl clashβ€œ, then it is possible that the requests ends up in the other view.

You thus should define another URL, or encode the post logic in the display view. In case of a POST request, you can thus first take the necessary steps, and then for example return a redirect to the page, such that we can again render the page:

def display(request, foo):
    if request.method == 'POST':
        # do something
        return redirect(display, foo=foo)
    #do something else (original code)
    return HttpResponse(..)

This is the famous Post/Redirect/Get web development design pattern [wiki]. This is usually better than returning a HTTP response directly in the POST, since if the user performs a refresh, the POST will be performed a second time.

1πŸ‘

As mentioned in the comment by @williem, you have two path() defined in the urls.py.

Always First matching route will be picked up from the url route table. So whenever r^’posts/’ is requested it will call the display() from the user_views, so it will never go to foobar(). Either remove the route with display() or change the sequence. Also, I assume you imported the user_views.

Reference:
https://docs.djangoproject.com/en/2.1/topics/http/urls/

πŸ‘€b107

Leave a comment