I try to make user registration in Django (version 2.2). I can run local server and fill register form in but when I click on submit button then there is diffrent redirecting and new user is not created. Instead of redirecting to main page ('/') there is redirecting to 'accounts/register/register' and I don't know why. Can somebody help me to find the problem.
Earlier in views.py I used import User from django.contrib.auth.models and changed the code in register.html but the problem was the same. Maybe something is wrong with my settings.py? in settings.py I added to INSTALLED_APPS = ['users.apps.UsersConfig',] and at the end LOGIN_REDIRECT_URL = '/'
# views.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm
def register(request):
if request.method == "POST":
form_register = UserCreationForm(request.POST)
if form_register.is_valid():
username = form_register.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
return redirect('register/')
else:
form_register = UserCreationForm()
return render(request, "registration/register.html", {'form_register': form_register})
# urls.py
from django.contrib import admin
from django.urls import path, include
from users.views import register
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/register/', register),]
# apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
# register.html
<!doctype html>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="register" method="POST">
{% csrf_token %}
{{form_register.as_p}}
<button type="submit">Register</button>
</form>
<div>
{% for message in messages %}
<h3> {{ message }} </h3>
{% endfor %}
</div>
</body>
</html>
Django isn't redirecting anywhere. Your actual register view is not even being called.
You have put the form action as "register". That's a relative path; since you started on "/accounts/register/" it will be appended to that, and that's where the form will submit to.
You should do one of:
action="/accounts/register/"
Better, give the URL pattern a name in urls.py: path('accounts/register/', register, name='register')
and refer to it with the url tag: action="{% url 'register' %}"
Or, just leave the action empty, in which case it will submit to the same page, which is what you want anyway: action=""
Thanks for help. I used all your advice but I have still the same problem. After trying to register a new user I have the following reply in terminal:
Not Found: /accounts/register/register
[31/Oct/2019 20:18:40] "POST /accounts/register/register HTTP/1.1" 404 3847
Not Found: /favicon.ico
[31/Oct/2019 20:18:40] "GET /favicon.ico HTTP/1.1" 404 2335
To solve problem with favicon I put in urls.py
from django.views.generic import RedirectView
url_patterns=[
path(r'^favicon\.ico$',RedirectView.as_view(url='/static/images/favicon.ico')),]
but it doesn't work. The worst problem is the new user is not created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.