[英]django: how to merge a class based view with a function based view
出於某種原因,我想在我的模板中使用基於 django class 的表單視圖“PasswordResetView”,它已經有一個基於 function 的視圖,這是主視圖,所以我認為我應該從 django 的源代碼中復制粘貼“PasswordResetView” views.py 文件並更改所需內容,但我總是遇到一些錯誤,因為我不熟悉基於 class 的視圖
這是我在 views.py 文件中的觀點:
def home(request):
user = request.user
signin_form = SigninForm()
signup_form = SignupForm()
if 'signin_form' in request.POST:
signin_form = SigninForm(request.POST)
if signin_form.is_valid():
email = request.POST['email']
password = request.POST['password']
user = authenticate(email=email, password=password)
if user:
login(request, user)
elif user is None:
messages.error(request, 'ُEmail or password is incorrect')
if 'signup_form' in request.POST:
signup_form = SignupForm(request.POST)
if signup_form.is_valid():
signup_form.save()
full_name = signup_form.cleaned_data.get('full_name')
email = signup_form.cleaned_data.get('email')
raw_password = signup_form.cleaned_data.get('password1')
account = authenticate(email=email, password=raw_password)
login(request, account)
context = {'signin_form': signin_form,'signup_form': signup_form}
return render(request, 'main/home.html', context)
這是 django 源代碼中的“PasswordResetView”:
from django.urls import reverse, reverse_lazy
from django.contrib.auth.tokens import default_token_generator
from django.views.generic.edit import FormView
from django.views.decorators.csrf import csrf_protect
from django.utils.decorators import method_decorator
class PasswordContextMixin:
extra_context = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': self.title,
**(self.extra_context or {})
})
return context
class PasswordResetView(PasswordContextMixin, FormView):
email_template_name = 'registration/password_reset_email.html'
extra_email_context = None
form_class = PasswordResetForm
from_email = None
html_email_template_name = None
subject_template_name = 'registration/password_reset_subject.txt'
success_url = reverse_lazy('password_reset_done')
template_name = 'registration/password_reset_form.html'
title = _('Password reset')
token_generator = default_token_generator
@method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def form_valid(self, form):
opts = {
'use_https': self.request.is_secure(),
'token_generator': self.token_generator,
'from_email': self.from_email,
'email_template_name': self.email_template_name,
'subject_template_name': self.subject_template_name,
'request': self.request,
'html_email_template_name': self.html_email_template_name,
'extra_email_context': self.extra_email_context,
}
form.save(**opts)
return super().form_valid(form)
INTERNAL_RESET_SESSION_TOKEN = '_password_reset_token'
在我的 urls.py 中
path('', views.home, name='home'),
您可以將基於 class 的視圖與基於 function 的視圖一起使用。 你只需要在urls.py
中寫下所有路由
from django.contrib.auth import views as auth_views
path('', views.home, name='home'),
path('password-reset', auth_views.PasswordResetView.as_view() ,name='password-reset'),
我假設您想從home view
訪問PasswordResetView
的表單而不更改頁面,如果是這樣,您不必為PasswordResetView
使用基於 class 的視圖。
您需要做的就是導入 django built in auth views
。
網址.py
from django.contrib.auth import views as auth_views
path('', views.home, name="home"), # your home page url
# password reset url
path('', auth_views.PasswordResetView.as_view(template_name="myapp/home.html"), name="password_reset"),
在你的模板 (home.html)
<form action="{% url 'password_reset' %}" method="POST">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Submit</button>
</form>
現在,無論何時單擊提交按鈕 django 都會將password_reset
url go 並為您完成工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.