Urls config in django-registration in Django - python

I am newbie Django dev, I have a bit problem about urls config. I want to make url http://localhost:8000/user-auth/register/
In my project, there is user_auth app with below urls:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^register$', views.register),
]
In register this url within urls in my site:
from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
url(r'^user-auth/', include('user_auth.urls')),
]
In my view user_auth/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .forms import RegisterForm
def register(request):
if request.method == 'POST':
response = HttpResponse()
response.write("<h1>Thanks for registering</h1></br>")
response.write("Your username:" + request.POST['username'])
response.write("Your email" + request.POST['email'])
return response
registerForm = RegisterForm()
return render(request, 'user_auth/register.html', {'form':registerForm})
In my user_auth/forms.py
from django import forms
class RegisterForm(forms.Form):
username = forms.CharField(label='Username', max_length=100)
password = forms.CharField(widget=forms.PasswordInput)
email = forms.EmailField(label='Email')
When I access to the link http://localhost:8000/user-auth/register/, the console announce "Not Found: /user-auth/register/". I dont know reason why and where. Could you please help me on this problem?. Tks

Related

Heroku cannot import name 'login' [duplicate]

I try to build a login function for my page. To edit the urls.py as followed, it keeps printing this:
cannot import name 'login' from 'django.contrib.auth.views'
How could I deal with the problem?
from django.contrib.auth.views import login
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path("login/",
login,
{"template_name": "users/login.html"},
name="login"),
]
Since django-1.11, the login, logout, etc. function-based views have been rewritten to class-based views: the LoginView [Django-doc] and LogoutView [Django-doc] classes, as is specified in the release notes. The "old" function-based views could still be used, but were marked as deprecated.
In django-2.1, the old function-based views have been removed, as specified in the release notes.
You can write it like:
from django.contrib.auth.views import LoginView
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path('login/',
LoginView.as_view(
template_name='users/login.html'
),
name="login"
),
]
try this
app_name = 'users'
urlpatterns = [
url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
]
You can try with this code:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns = [
# Login page.
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]
#Willem Van Onsem's answer worked for me. On an implementation note, if you rather keep your view code separate from urls (also if you have some processing to do), you would write your urls.py like this (based on a per-app urls.py file in your app folder which means you have to include it in the overall urlpatterns of the project's urls.py file which is in the same folder as your settings.py file, with the syntax path('', include('users.urls')),):
from django.urls import path
from .views import (
login_view
)
app_name = "userNamespace"
urlpatterns = [
path('login/', loginView.as_view(), name="login-view"),
]
and over in your views.py file you would have something like this:
from django.shortcuts import render
from django.contrib.auth.views import (
LoginView,
)
from users.models import User
class login_view(LoginView):
# The line below overrides the default template path of <appname>/<modelname>_login.html
template_name = 'accounts/login.html' # Where accounts/login.html is the path under the templates folder as defined in your settings.py file
Can try this to create a login form
# views page
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.contrib import messages
def loginPage(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.info(request, 'Username or Password is incorrect')
context = {}
return render(request, 'accounts/login.html', context)
#urls
urlpatterns = [
path('login/', views.loginPage, name='login'),,
]

Django cannot import login from django.contrib.auth.views

I try to build a login function for my page. To edit the urls.py as followed, it keeps printing this:
cannot import name 'login' from 'django.contrib.auth.views'
How could I deal with the problem?
from django.contrib.auth.views import login
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path("login/",
login,
{"template_name": "users/login.html"},
name="login"),
]
Since django-1.11, the login, logout, etc. function-based views have been rewritten to class-based views: the LoginView [Django-doc] and LogoutView [Django-doc] classes, as is specified in the release notes. The "old" function-based views could still be used, but were marked as deprecated.
In django-2.1, the old function-based views have been removed, as specified in the release notes.
You can write it like:
from django.contrib.auth.views import LoginView
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path('login/',
LoginView.as_view(
template_name='users/login.html'
),
name="login"
),
]
try this
app_name = 'users'
urlpatterns = [
url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
]
You can try with this code:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns = [
# Login page.
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]
#Willem Van Onsem's answer worked for me. On an implementation note, if you rather keep your view code separate from urls (also if you have some processing to do), you would write your urls.py like this (based on a per-app urls.py file in your app folder which means you have to include it in the overall urlpatterns of the project's urls.py file which is in the same folder as your settings.py file, with the syntax path('', include('users.urls')),):
from django.urls import path
from .views import (
login_view
)
app_name = "userNamespace"
urlpatterns = [
path('login/', loginView.as_view(), name="login-view"),
]
and over in your views.py file you would have something like this:
from django.shortcuts import render
from django.contrib.auth.views import (
LoginView,
)
from users.models import User
class login_view(LoginView):
# The line below overrides the default template path of <appname>/<modelname>_login.html
template_name = 'accounts/login.html' # Where accounts/login.html is the path under the templates folder as defined in your settings.py file
Can try this to create a login form
# views page
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.contrib import messages
def loginPage(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.info(request, 'Username or Password is incorrect')
context = {}
return render(request, 'accounts/login.html', context)
#urls
urlpatterns = [
path('login/', views.loginPage, name='login'),,
]

error "The current path, user_info/, didn't match any of these."

Well, this question has been asked by many users, and I have tried some of the resolution provided in the query, but that doesn't seem to resolve my issue, maybe I am doing some other mistake.
I am trying to create a simple user registration/login/logout forms
SchoolnSkill ---Project name, and below is the SchoolnSkill/url details:-
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^user_info/', include('user_info.urls')),
]
user_info--App name, and the user_info/url details are below:-
from django.conf.urls import url
from . import views
urlpatterns = [
# url(r'^$', views.index, name='index'),
url(r'^registration/', views.UserFormView, name='registration')
]
My views.py output is as below:-
from django.shortcuts import render, redirect
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth import authenticate, login
from django.views import generic
from .models import Registration
from django.views import View
from .forms import UserForm
class UserFormView(View):
#from_class = UserForm
template_name = 'user_info/registration_form.html'
#dispaly blank form, new user coming to the website
def get(self, request):
form = self.UserForm(None)
return render(request, self.template_name, {'form':form})
def post(self, request):
form = self.UserForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
form.py content as below:
from django.contrib.auth.models import User
from django import forms
from django.forms import ModelForm
class UserForm(forms.ModelForm):
password = forms.CharField(widget =forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password']
I am using below versions:
Python 3.6
Django 1.11.3
Spyder IDE
I am pretty sure that am doing something silly.
Please help.
below is the error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/user_info/
Using the URLconf defined in SchoolnSkill.urls, Django tried these URL patterns, in this order:
^admin/
^user_info/ ^registration/ [name='registration']
The current path, user_info/, didn't match any of these.
as you are using class based view you need to add as_view in the url
so
change this in user_info url to
urlpatterns = [
url(r'^registration/', views.UserFormView.as_view(), name='registration')
]

Django keeps rendering the same page

I am trying to set up navigation with Django, however, each time I try to navigate it goes back to the same page again.
Please help, any advice will be appreciated. Thank you!
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
#from django.contrib.auth import views as auth_views
#from . import views
# Create your views here.
def login(request):
return render(request, 'login.html')
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
#username = form.cleaned_data.get('username')
#raw_password = form.cleaned_data.get('password1')
#user = authenticate(username=username, password=raw_password)
#login(request, user)
return redirect('/templates')
else:
form = UserCreationForm()
return render(request, 'templates/signup_form.html', {'form': form})
in Project the file called
urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^login/', include('website.urls')),
url(r'^signup/', include('website.urls')),
url(r'^admin/', admin.site.urls),
]
in the app website urls.py
from django.conf.urls import url
from django.contrib.auth.views import login
from . import views
#
urlpatterns = [
url(r'^', views.login, name=''),
url(r'^login/', login, {'template_name': 'templates/login.html'}),
url(r'^signup/', views.signup, name = 'signup')
#url(r'^login/$', index,{{'template_name': 'templates/index.html' }})
]
The URL patterns are matched from top to bottom. Your first urlpattern is matching anything it sees, so django sends all requests to views.login.
Try putting that row at the bottom of the list
urlpatterns = [
url(r'^login/', login, {'template_name': 'templates/login.html'}),
url(r'^signup/', views.signup, name = 'signup')
url(r'^', views.login, name=''),}})
]

Django 1.9.1 NoReverseMatch when using django.auth

I'm trying to learn django by following the book "Django by Example" and probably due to conflicting versions i'm running into this problem when trying to use django.auth together with some URL settings in
settings.py.
Im getting totally frustrated by now since i have no idea how to even begin debugging this error. Any help or advice would be much appreciated
Here's the relevant part of the settings.py file
from django.core.urlresolvers import reverse_lazy
LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_URL = reverse_lazy('logout')
app views.py:
from django.shortcuts import render, redirect
from django.shortcuts import HttpResponse
from django.contrib.auth import authenticate, login, logout
from .forms import LoginForm
from django.contrib.auth.decorators import login_required
# Create your views here.
#login_required
def dashboard(request):
return render(request, 'account/dashboard.html', {'section': 'dashboard'})
urls.py
from django.conf.urls import url
from . import views
app_name = 'account'
urlpatterns = {
url(r'^$', views.dashboard, name='dashboard'),
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login', name='logout_then_login'),
}
Main urls.py :
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/', include('account.urls')),
]
Error Message
updated settings.py :
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_URL = reverse_lazy('account:logout')
When you use app_name that sets up a namespace that will be used when you include() that urls.py somewhere else.
So there's no url with the name "login", instead it's called "account:login", and that's the name you have to pass to reverse().
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_URL = reverse_lazy('account:logout')
Relevant docs: URL namespaces and included URLconfs
If you are using django-extensions (you should), you can use the management command show_urls to get a nicely formatted list of all the url routes that are registered in your project.

Categories

Resources