目前,我有两种样式的模板:用户+客户和用户+公司。我想创建一个视图,以便从这两个相关模板中的一个创建用户+帐户。
当前我已经实现了,但是有一个问题:代码似乎非常肿,而且我也不知道是否有CBV来编辑带有相关模型的模型,那么这将导致其他视图也肿。
有什么方法可以改善这一点吗?
models.py:https://pastebin.com/9Fp0F6CG
我的views.py:
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from django.views import generic
from .forms import UserForm, ClientForm, CompanyForm
class ClientFormView(generic.View):
def get(self, request, *args, **kwargs):
template_name = "users/registration/form_client.html"
context = {"form_user": UserForm, "form_client": ClientForm}
return render(request, template_name, context)
def post(self, request, *args, **kwargs):
template_name = "users/registration/form_client.html"
context = {"form_user": UserForm, "form_client": ClientForm}
form_user = UserForm(request.POST)
form_client = ClientForm(request.POST)
if form_user.is_valid() and form_client.is_valid():
# get data for auth and login
email = form_user.cleaned_data["email"]
password_raw = form_user.cleaned_data["password1"]
# add user_type = client
instance_user = form_user.save(commit=False)
instance_user.user_type = "cl"
instance_user.save()
instance_client = form_client.save(commit=False)
user = authenticate(email=email, password=password_raw)
if user is not None:
# add the user in related user field
instance_client.user = user
instance_client.save()
login(request, user)
return redirect("main:home")
return render(request, template_name, context)
class CompanyFormView(generic.View):
def get(self, request, *args, **kwargs):
template_name = "users/registration/form_company.html"
context = {"form_user": UserForm, "form_company": CompanyForm}
return render(request, template_name, context)
def post(self, request, *args, **kwargs):
template_name = "users/registration/form_company.html"
context = {"form_user": UserForm, "form_company": CompanyForm}
form_user = UserForm(request.POST)
form_company = CompanyForm(request.POST)
if form_user.is_valid() and form_company.is_valid():
# get data for auth and login
email = form_user.cleaned_data["email"]
password_raw = form_user.cleaned_data["password1"]
# add user_type = client
instance_user = form_user.save(commit=False)
instance_user.user_type = "comp"
instance_user.save()
instance_company = form_company.save(commit=False)
user = authenticate(email=email, password=password_raw)
if user is not None:
# add the user in related user field
instance_company.user = user
instance_company.save()
login(request, user)
return redirect("main:home")
return render(request, template_name, context)
答案 0 :(得分:0)
我仅对视图进行了一些改进,没有查看模型。
generic.View
更改为
generic.CreateView
,因为您正在创建东西。generic.CreateView
导出时,您可以将
template_name
和context
中的函数,并将它们放在
课,因为 GET 和
POST 操作。render
函数,只需调用
应该为您处理渲染的super
。有了上面的建议,它应该看起来像这样。您可能需要对其进行调整,但我无法对其进行全面测试。
from extra_views import CreateWithInlinesView, InlineFormSet
class ClientCompanyMixin(object):
def create_user(self, form_user, form_second, type):
# get data for auth and login
email = form_user.cleaned_data["email"]
password_raw = form_user.cleaned_data["password1"]
# add user_type = client/company
instance_user = form_user.save(commit=False)
instance_user.user_type = type
instance_user.save()
instance = form_second.save(commit=False)
user = authenticate(email=email, password=password_raw)
return instance, user
class UserInline(InlineFormSet):
model = User
class ClientFormView(CreateWithInlinesView, ClientCompanyMixin):
template_name = "users/registration/form_client.html"
model = Client
inlines = [UserInline]
def get_context_data(self, **kwargs):
context = super(ClientFormView, self).get_context_data(**kwargs)
context["form_user"] = UserForm
context["form_client"] = ClientForm
return context
def post(self, request, *args, **kwargs):
form_user = UserForm(request.POST)
form_client = ClientForm(request.POST)
if form_user.is_valid() and form_client.is_valid():
instance_client, user = self.create_user(form_user, form_client, "cl")
if user is not None:
# add the user in related user field
instance_client.user = user
instance_client.save()
login(request, user)
return redirect("main:home")
return super(ClientFormView, self).post(request, *args, **kwargs)
class CompanyFormView(CreateWithInlinesView, ClientCompanyMixin):
template_name = "users/registration/form_company.html"
model = Company
inlines = [UserInline]
def get_context_data(self, **kwargs):
context = super(CompanyFormView, self).get_context_data(**kwargs)
context["form_user"] = UserForm
context["form_company"] = CompanyForm
return context
def post(self, request, *args, **kwargs):
form_user = UserForm(request.POST)
form_company = CompanyForm(request.POST)
if form_user.is_valid() and form_company.is_valid():
instance_company, user = self.create_user(form_user, form_company, "comp")
if user is not None:
# add the user in related user field
instance_company.user = user
instance_company.save()
login(request, user)
return redirect("main:home")
return super(CompanyFormView, self).post(request, *args, **kwargs)