我想传递request.user以使用ModelMultipleChoiceField以表单形式显示当前用户。我可以通过https://stackoverflow.com/a/25184373/6568309来解决我的问题。我修改了我的代码,如下所示。
但我只能通过基于函数的视图获得解决方案。好吧,我一直使用基于类的视图,因为我可以使用通用视图,并且首先推荐它。 是否可以通过基于类的视图传递request.user(使用FormView或ModelFormView)?另外,基于函数的视图和基于类的视图混合是否正常以满足您在Django中的需求?
提前谢谢。
forms.py
class CustomForm(forms.Form):
username = forms.CharField(initial='testname',max_length=150)
email = forms.EmailField()
phone_number = forms.CharField(max_length=15)
position = forms.CharField(max_length=15)
uperall = forms.ModelMultipleChoiceField(queryset=None)
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super(CustomForm, self).__init__(*args, **kwargs)
self.fields['uperall'].queryset = User.objects.filter(username=user.username)
urls.py
urlpatterns = [
url(r'^$', UserList.as_view(), name='index'),
url(r'^create/$', UserCreate.as_view(), name='create'),
url(r'^test/$', TestView.as_view(), name='test'),
url(r'^test1/$', views.ftestview, name='test1'),
]
views.py
def ftestview(request):
if request.method == 'POST':
form = CustomForm(request.POST, user=request.user)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
phone_number = form.cleaned_data['phone_number']
position = form.cleaned_data['position']
with transaction.atomic():
user = User.objects.create(username=username,email=email)
userinfo = UserInfo.objects.create(user=user,phone=phone_number,position=position)
userinfo.save()
user.save()
return HttpResponseRedirect('/success')
else:
form = CustomForm(user=request.user)
return render(request, 'manager/alltoall.html', { 'form': form })
根据levi的回答,感谢。我改变了我的代码,如下所示
views.py
class TestView(FormView):
form_class = CustomForm
template_name = 'manager/alltoall.html'
def get_form_kwargs(self):
user = self.request.user
form_kwargs = super(TestView, self).get_form_kwargs()
form_kwargs.update({'initial': {'uperall': User.objects.filter(username=user.username)}})
return form_kwargs
def form_valid(self, form):
username = form.cleaned_data['username']
email = form.cleaned_data['email']
phone_number = form.cleaned_data['phone_number']
position = form.cleaned_data['position']
with transaction.atomic():
user = User.objects.create(username=username,email=email)
userinfo = UserInfo.objects.create(user=user,phone=phone_number,position=position)
userinfo.save()
user.save()
return super(TestView, self).form_valid(form)
但我收到的错误如下。
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/manager/test/
Django Version: 1.10.1
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'manager.apps.ManagerConfig',
'mptt']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template C:\Users\Jaemyun\django_real\apa\apa\manager\templates\manager\alltoall.html, error at line 9
'NoneType' object has no attribute 'all' 1 : <select multiple="multiple" data-field-name="groups">
2 : <option value="volvo">Volvo</option>
3 : <option value="hyundai">Hyundai</option>
4 : </select>
5 :
6 : <form action="." method="post">
7 : {% csrf_token %}
8 : <table>
9 : {{ form }}
10 : </table>
11 : <input type="submit" value="Submit" />
12 : </form>
Traceback:
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response
217. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response
215. response = response.render()
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\response.py" in render
109. self.content = self.rendered_content
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\response.py" in rendered_content
86. content = template.render(context, self._request)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\backends\django.py" in render
66. return self.template.render(context)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\base.py" in render
208. return self._render(context)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\base.py" in render
1050. return render_value_in_context(output, context)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\template\base.py" in render_value_in_context
1028. value = force_text(value)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\utils\encoding.py" in force_text
76. s = six.text_type(s)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\utils\html.py" in <lambda>
391. klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\forms.py" in __str__
123. return self.as_table()
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\forms.py" in as_table
271. errors_on_separate_row=False)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\forms.py" in _html_output
226. 'field': six.text_type(bf),
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\utils\html.py" in <lambda>
391. klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\boundfield.py" in __str__
43. return self.as_widget()
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\boundfield.py" in as_widget
101. return force_text(widget.render(name, self.value(), attrs=attrs))
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\widgets.py" in render
610. options = self.render_options(value)
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\widgets.py" in render_options
560. for option_value, option_label in self.choices:
File "C:\Users\Jaemyun\django_real\apa\myenv\lib\site-packages\django\forms\models.py" in __iter__
1114. queryset = self.queryset.all()
Exception Type: AttributeError at /manager/test/
Exception Value: 'NoneType' object has no attribute 'all'
答案 0 :(得分:3)
您可以从get_form_kwargs
班级视图覆盖方法FormView
,以便为您的表单设置初始数据。
class YouFormView(FormView):
teplate_name = 'your template'
form_class = YourForm
def get_form_kwargs(self):
user = self.request.user
form_kwargs = super(YouFormView, self).get_form_kwargs()
form_kwargs.update({
'initial': {
'uperall': User.objects.filter(username=user.username)
}
})
return form_kwargs
关于混合视图:我建议坚持基于类的视图。