我是django的新手,我被要求将当前登录的用户与用户创建的Context(我的模型类)相关联。
如何/在何处检索用户(来自request.user
)对象?如何在模型中设置它?
是否可以将request.user
传递给使用通用视图的视图?
这是我的Context类:
from django.contrib.auth.models import User
class Context(models.Model):
title = models.CharField(max_length=32)
description = models.TextField()
user = models.ForeignKey(User,null=False)
以下是表格:
class ContextForm(ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit'))
super(ContextForm, self).__init__(*args, **kwargs)
class Meta:
model = Context
exclude = ('user')
来自urls.py的网址:
url(r'^create/$', CreateView.as_view(model=Context, form_class=ContextForm),name='context_create'),
最后,context_form.html以脆弱的形式
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
{% endblock %}
提前感谢您的帮助。
答案 0 :(得分:1)
查看django-braces!它有一个form mixin从kwargs弹出当前用户。对于基于类的视图,还有mixin将用户传递给表单。 如果您正在进行基于类的视图,只需使用forms.py和views.py中提供的mixins,并覆盖save()方法。
forms.py:
from braces.forms import UserKwargModelFormMixin
class ContextForm(UserKwargModelFormMixin, ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit'))
super(ContextForm, self).__init__(*args, **kwargs)
class Meta:
model = Context
fields = ('title', 'description',)
def save(self, force_insert=False, force_update=False, commit=True):
obj = super(ContextForm, self).save(commit=False)
obj.user = self.user
if commit:
obj.save()
return obj
views.py
from braces.views import UserFormKwargsMixin
class CreateContextView(UserFormKwargsMixin, CreateView)
model = Context
form_class = ContextForm
urls.py:
url(r'^create/$', CreateContextView.as_view(),name='context_create'),
你也可以这样做:
forms.py:
class ContextForm(ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None)
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit'))
super(ContextForm, self).__init__(*args, **kwargs)
class Meta:
model = Context
fields = ('title', 'description',)
def save(self, force_insert=False, force_update=False, commit=True):
obj = super(ContextForm, self).save(commit=False)
obj.user = self.user
if commit:
obj.save()
return obj
views.py
def some_view(request):
...
form = ContextForm(request.POST, user=request.user)
...
答案 1 :(得分:0)
在你的模特中:
from django.contrib.auth.models import User
class Context(models.Model):
title = models.CharField(max_length=32)
description = models.TextField()
user = models.OneToOneField(User)
在与用户关联的视图中:
if request.method == 'POST':
form = ContextForm(request.POST)
if form.is_valid():
user = User.objects.create_user(username=form.cleaned_data['username'], email = form.cleaned_data['email'], password = form.cleaned_data['password'])
user.save()
obj = Context(user=user, title=form.cleaned_data['title'], description=form.cleaned_data['description'])
obj.save()
如果您需要登录,只需添加 @login_required
@login_required
def myfunction(request):
...
修改强>
我看过你编辑的问题,你需要将它添加到你的urls.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)
这意味着它将传递给使用RequestContext呈现的所有模板,所有模板都是通用视图。