Creating Forms that Redirects to Different URL When Successful

So I have created two folders (Aside from my main project folder) One of my folders is the main app and the second folder is a users folder.


Error Message:

Page not found (404) Request Method: POST Request URL: http://127.0.0.1:8000/users/register Using the URLconf defined in cooknook.urls, Django tried these URL patterns, in this order:

admin/

users/ register/ [name='cooknook-register']

[name='cooknook-home']

about/ [name='cooknook-about']

account/ [name='cooknook-account']

The current path, users/register, didn’t match any of these.


^^ Error Thrown

So In my views.py inside of my users folder I am telling it after success:

`

def register(response):
    if response.method == "POST":
        form = UserCreationForm()
        if form.is_valid():
            form.save()`
        
        return redirect('cooknook-home')


Basically, I did already try to outsource the problem and tried to implement 'reverse' function in this which did not seem to work at all. The problem is that it seems it is trying to find the urls/html(temps) inside of my cooknookapp folder which is my main app folder. I'm needing it to redirect to home.html (located inside of my main app folder) How can I get Django to look outside of the Users/ Dir for this?


register.html (inside users folder)

{% extends 'cooknookapp/base.html' %}

{% block title %}
    Create an Account
{% endblock %}

{% block content %}
    <form method="POST" class="form-group">
        {{form}}
        <button type="submit" class-"btn-submit">Sign Up</button>
    </form>
{% endblock %}

cooknookapp (main app) urls.py:

> from django.urls import path from . import views
> 
> urlpatterns = [
>     path('', views.home, name='cooknook-home'),
>     path('about/', views.about, name='cooknook-about'),
>     path('account/', views.account, name='cooknook-account'), ]

user (current dir folder) urls.py:

> from django.urls import path from . import views
> 
> # Added URL Path urlpatterns = [
>     path('register/', views.register, name='cooknook-register'), ]

I was expecting for it to send me to the home page which is located in the cooknookapp folder. Instead, it looked for this url in the users/ folder which is where the code that is being written is in. If you need any additional information from me to help, please don't hesitate to let me know.

from django.shortcut import resolve_url
def register(response):
    if response.method == "POST":
        form = UserCreationForm()
        if form.is_valid():
            form.save()`
            return redirect(resolve_url('cooknook-home'))
        else:
            # invaild action


The error being given is actually a 404 for the /users/register page itself, not the redirect to "home"

To include the register urls, you will need to add something like this to your main (top-level) urls.py

 path("users/", include("users.urls")),

This will add all the urls in /users/urls.py to the checklist for possible matches. The first argument "users/" says that all these urls from that file will be preceeded by 'users' - so users/register will match, but 'register' alone won't.

relevant Django Docs info

Вернуться на верх