regist() got an unexpected keyword argument 'template_name' - python

i got a error that
TypeError at /accounts/regist/
regist() got an unexpected keyword argument 'template_name' .
i can understand there is no variable in my file.
but,in login,no template_name file can be worked.
So,i cannot know how to fix it.
I wrote in urls.py of accounts,
from django.conf.urls import url
from . import views
from django.contrib.auth.views import login, logout
urlpatterns = [
url(r'^login/$', login,
{'template_name': 'registration/accounts/login.html'},
name='login'),
url(r'^logout/$', logout, name='logout'),
url(r'^regist/$', views.regist,
{'template_name': 'registration/accounts/regist.html'},
name='regist' ),
url(r'^regist_save/$', views.regist_save, name='regist_save'),
]
in views.py
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.decorators.http import require_POST
from .forms import RegisterForm
def index(request):
context = {
'user': request.user,
}
return render(request, 'accounts/index.html', context)
#login_required
def profile(request):
context = {
'user': request.user,
}
return render(request, 'accounts/profile.html', context)
def regist(request):
form = RegisterForm(request.POST or None)
context = {
'form': form,
}
return render(request, 'accounts/regist.html', context)
#require_POST
def regist_save(request):
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
return redirect('main:index')
context = {
'form': form,
}
return render(request, 'accounts/regist.html', context)
if i have to define template_name,which file should i write it and how?
Are there differences to make system login page and regist page?

You don't have to specify a template_name when you are using your own function-based view, so just remove the {'template_name': 'registration/accounts/regist.html'} bit from the registration URL.

Related

Django Login Required Redirect

Im trying to redirect a user when he isn't logged in the my LoginPage.
I Have Tried Many Different solutions - Like #login_required & request.user.is_authenticated - yet nothing worked...
im leaving my views.py & urls.py so someone can see if they can spot the problem
Thanks For Any Help!
Views.Py:
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render,redirect
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
# Create your views here.
#login_required(login_url='/Login')
def Dashboard(request):
if request.user.is_authenticated:
return render(request, 'home/index.html')
else:
return LoginPage(request)
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:
request.session.set_expiry(86400)
login(request, user)
return redirect('dashboard')
return render(request,'accounts/login.html')
def registerPage(request):
form = UserCreationForm()
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, "Account Created for " + user + " Succesfully" )
return redirect('login')
context = {'form': form}
return render(request, 'accounts/register.html', context)
def Admin(request):
return HttpResponseRedirect(reverse('admin:index'))
def Logout(request):
authenticate.logout(request)
logout(request)
return LoginPage(request)
Urls.Py:
from django.urls import path
from. import views
from django.contrib import admin
from django.urls import path, reverse_lazy
from django.views.generic.base import RedirectView
urlpatterns = [
path('',views.LoginPage),
path("Login", views.LoginPage, name="login"),
path("Register", views.registerPage,name="register"),
path("admin", views.Admin, name="admin"),
path("dashboard", views.Dashboard, name="dashboard"),
path('', RedirectView.as_view(url=reverse_lazy('admin:index'))),
path('admin/', admin.site.urls),
path("Login", views.logout, name="logout"),
Hlooo
Instead of this
#login_required(login_url='/Login')
Try this
#login_required(login_url='login')
use small 'l' since you used small 'l' in name = 'login'
This worked in my project , if user is not authenticated he will be
redirected to login page
In your settings.py, try add
LOGIN_URL = '/Login/'
also after logout, send them to the login page
LOGOUT_URL = '/Login/'

Trying a write a simple program and keep getting this error

Error : TemplateDoesNotExist at /1
forms.py
class SpotifyForm(forms.Form):
SpotifyForm = forms.CharField(label= "Spotify Form")
views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views import View
from .forms import SpotifyForm
from .api import SpotifyAPI
from .forms import ClientId
class Search(View):
form = SpotifyForm
initial = {"initial" : "Error"}
template_name = "homepage.html"
context = {"form": form}
def get(self, request, *args, **kwargs):
form = self.form(initial = self.initial)
return render(request , self.template_name, self.context)
urls.py
from django.urls import path
from .views import Search
urlpatterns = [
path("1" , Search.as_view())
]
templates directory: spotify_client\spotify_calls\spotify_calls\templates
urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path("" , include("spotify_calls.urls"))
]
Basically, all I am doing is trying to access the Spotify API in the views.py(not done yet), just wanted to render the html to at least see if the form will show but nothing did. Also, all recommendations with class based views will be appreciated since this is my first time
according to the Django docs :
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views import View
from .forms import MyForm
class MyFormView(View):
form_class = MyForm
initial = {'key': 'value'}
template_name = 'form_template.html'
def get(self, request, *args, **kwargs):
form = self.form_class(initial=self.initial)
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
# <process form cleaned data>
return HttpResponseRedirect('/success/')
return render(request, self.template_name, {'form': form})

HttpResponseRedirect в Django. Can't do redirect on 'done' page

After filling the page should appear 'done', but i have an error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/candidate/done.html
The current path, candidate/done.html, didn't match any of these.
Can't configure redirect to 'done' page.
Here views.py:
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import AnketaForm
from .models import Anketa
def anketa_create_view(request):
if request.method == 'POST':
form = AnketaForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('candidate/done.html')
else:
form = AnketaForm()
return render(request, 'candidate/anketa_create.html', {'form': form})
urls.py (apps/candidate)
from django.urls import path
from . import views
urlpatterns = [
path('', views.anketa_create_view, name = 'anketa_create_view'),
]
urls.py
from django.contrib import admin
from django.urls import path, include
from candidate.views import anketa_create_view
urlpatterns = [
path('done/', anketa_create_view),
path('', anketa_create_view),
path('grappelli/', include('grappelli.urls')),
path('admin/', admin.site.urls),
]
you need to give a name to the done url
path('done/', anketa_create_view, name='done'),
and can do with reverse
return HttpResponseRedirect(reverse('done'))
or you can do redirect shortcut
return redirect('done')
Remove in urls.py
path('done/', anketa_create_view)
and replace in views.py
return HttpResponseRedirect('candidate/done.html')
to
return render(request, 'candidate/done.html', {})

Please why is there a circular import error

I am working on a django project, but it returns the included urlconf "myapp.urls"does not appear to have any patterns in it.
I tried checking my views to ensure I imported everything correctly
from django.contrib import admin
from django.urls import path
from .views import home
from accounts.views import login_view
urlpatterns = [
path('admin/', admin.site.urls),
path('',home),
path('accounts/login/', login_view),
]
I expect the site to run and redirect me to the login page
This is my views in the same directory with the urls.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
#login_required
def home(request):
return render(request,"home.html")
This is the views.py for the accounts.
from django.shortcuts import render,redirect
from django.contrib.auth import(
authenticate,
get_user_model,
login,
logout
)
from .forms import UserLoginForm, UserRegisterForm
def login_view(request):
next = request.GET.get('next')
form = UserLoginForm()
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username,password=password)
login(request,user)
if next:
return redirect(next)
return redirect("/")
context = {
'form': form,
}
return render(request, "login.html",context)
When I run your project on Django 2.2, I don't see a circular import. Instead I see the error:
django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form UserRegisterForm needs updating.
Looking at your form, you haven't set fields, you have model = Userfields = [...] instead.
class UserRegisterForm(forms.ModelForm):
...
class Meta:
model = Userfields = [
'username',
'email',
'password',
"c_password"
]
Change it so that you set fields. You can remove 'password' and 'c_password' since you define these on your form separately.
class UserRegisterForm(forms.ModelForm):
...
class Meta:
fields = [
'username',
'email',
]

Django NameError: name 'password_reset_done' is not defined

found some topics like this but they are older or not solved.
Well, I'm following a youtube tutorial and I'm stuck in this part of it, I already know it's due the difference of version, I've gone to the docs and got some answers but still can't solve it by myself.
I'll post what I think it's relevant but if you guys want another part of my code ask and I'll get it.
the error as I said (on Traceback) is: NameError: name 'password_reset_done' is not defined
On the tutorial he didn't done anything on views.py about this, he just added some imports on url.py that was deprecated so I fix that but the errors keeps the same...
My accounts/urls.py:
from django.urls import path
from . import views
from django.contrib.auth.views import (
login,
logout,
password_reset,
PasswordResetDoneView,
PasswordResetConfirmView,
)
urlpatterns = [
path('', views.home),
path('login/', login, {'template_name': 'contas/login.html'}),
path('logout/', logout, {'template_name': 'contas/logout.html'}),
path('register/', views.register, name='register'),
path('perfil/', views.view_perfil, name='view_perfil'),
path('perfil/edit/', views.edit_perfil, name='edit_perfil'),
path('trocar-password/', views.trocar_password, name='trocar_password'),
path('reset-password/', password_reset, name='reset_password'),
path('reset-password/done/', password_reset_done, name='password_reset_done'),
path('reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/',
password_reset_confirm, name='password_reset_confirm'),
]
just to add some more code. My views.py:
from django.shortcuts import render, redirect, HttpResponse
from contas.forms import (
RegistrationForm,
EditPerfilForm,
)
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm, PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
def home(request):
numbers = [1,2,3,4,5]
name = 'Lucas Cyrne'
args = {'myName': name, 'numbers': numbers}
return render(request, 'contas/home.html', args)
def register(request):
if request.method=='POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return redirect('/contas')
else:
form = RegistrationForm()
args = {'form':form}
return render(request, 'contas/reg_form.html', args)
def view_perfil(request):
args = {'user': request.user}
return render(request, 'contas/perfil.html', args)
def edit_perfil(request):
if request.method=='POST':
form = EditPerfilForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect('/contas/perfil')
else:
form = EditPerfilForm(instance=request.user)
args = {'form': form}
return render(request, 'contas/edit_perfil.html', args)
def trocar_password(request):
if request.method=='POST':
form = PasswordChangeForm(data=request.POST, user=request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return redirect('/contas/perfil')
else:
return redirect('/contas/trocar_password')
else:
form = PasswordChangeForm(user=request.user)
args = {'form': form}
return render(request, 'contas/trocar_password.html', args)
Django looks for everywhere, and doesn't see password_reset_done
and above your urls patter, there is this:
NOTE: these built-in CBV are available forn Django >= 1.11
from django.contrib.auth.views import (
login,
logout,
password_reset, # suggestion: PasswordResetView
PasswordResetDoneView,
PasswordResetConfirmView,
)
you view is PasswordResetDoneView
instead of:
path('reset-password/done/', password_reset_done, name='password_reset_done'),
it should be:
path('reset-password/done/', PasswordResetDoneView.as_view(), name='password_reset_done'),
Cause you didn't call password_reset_done view, but using it in urls.
path('reset-password/done/', password_reset_done, name='password_reset_done'),
Look this url, you are trying to use view that doesn't exist.
Make your own view inheriting PasswordResetDoneView or, just use it without registering url. (You only can call url using password_reset_done like this
reverse('password_reset_done')
p.s. you have to add django.contrib.auth.urls to your urls.py. like
url('', include('django.contrib.auth.urls')), ( for django =< 1.11 )
You need to add app_name is urls.py
Add views as viewname.as_view() in urls.py

Categories