我可能会过度思考这个或者不是。我有一个注册表单,我想在索引页面上使用,并作为一个独立的表单。出于某种原因,独立表单工作得很好,但索引页面上的表单在提交时给出了403错误。我将在稍后使用jQuery,但是我需要在没有JavaScript的情况下工作。
呈现与Facebook主页类似的注册表单
$(document).ready(function() {
$('#arena').hover(function() {
$('#intro').fadeOut('slow');
});
});
我实际上使用具有不同动作属性的相同确切形式。我尝试使用相同的属性值交换属性值,并且不使用任何属性值。我似乎无法从索引页面访问我的视图,即使我在其中进行硬编码我得到了403错误。基本上,访问者应该能够在主页(如Facebook)或注册页面(如Twitter)上注册。我想我可能不得不使用信号来完成这项工作。
# views.py snippet
class IndexPageTemplateView(TemplateView): # index page(http://localhost)
template_name = 'fld/index.html'
def get(self, request, *args, **kwargs):
# prevent logged in users from accessing welcome page
if self.request.user.is_authenticated():
return HttpResponse(reverse("member_homepage", args=some_args))
else:
return render_to_response(self.template_name)
# Account view
class AccountCreateView(CreateView): # standalone(http://localhost/signup)
model = Account
form_class = CreateAccountForm
template_name = 'fld2/account_form'
success_url = 'thanks'
def get(self, request, *args, **kwargs):
# prevent logged in users from seeing this form
if self.request.user.is_authenticated():
# get the username of logged in user
...
# redirect to homepage
return HttpResponseRedirect(reverse('some_args', args=(args,)))
else:
# display form to non members
return render(request, self.template_name, {'form': self.get_form_class()})
def form_valid(self, form):
clean = form.cleaned_data
form.instance = form.save()
if form.instance:
try:
# send email
except Account.DoesNotExist:
...
return super(AccountCreateView, self).form_valid(form)
<!--index.html snippet-->
<form action="{% url "create_account" %}" method="post">{% csrf_token %}
<input name="username" type="text" required>
<input name="password" type="password" required>
<input name="email" type="email" required>
<input name="signup1" type="submit" value="Create account">
</form>
main urls.py代码段
<!--account_form.html-->
<form action="." method="post">{% csrf_token %}
<input name="username" type="text" required>
<input name="password" type="password" required>
<input name="email" type="email" required>
<input name="signup2" value="Create account" type="submit">
</form>
account urls.py
url(
regex=r'^$',
view=WelcomePageTemplateView.as_view(),
name='index'
),
url(
regex=r'^signup$',
view=include('account.urls'),
# name='signup'
),
url(
regex=r'^(?P<slug>[\w]+)/settings/?',
view=include('account.urls'),
)
答案 0 :(得分:2)
{% csrf_token %}
标记需要访问模板中的请求对象。您在render
中使用了AccountCreateView
,其中包含默认情况下的请求,但在您的IndexPageTemplateView
中使用了render_to_response
,但未使用render
。
修复索引视图的最简单方法是使用render_to_response
而不是def get(self, request, *args, **kwargs):
# prevent logged in users from accessing welcome page
if self.request.user.is_authenticated():
return HttpResponse(reverse("member_homepage", args=some_args))
else:
return render(request, self.template_name)
。
get
当您覆盖基于类的视图时,覆盖post
和super()
通常是个坏主意,因为您最终会复制(或破坏)功能。在您的情况下,最好调用def get(self, request, *args, **kwargs):
# prevent logged in users from accessing welcome page
if self.request.user.is_authenticated():
return HttpResponse(reverse("member_homepage", args=some_args))
else:
return super(IndexPageTemplateView, self).get(request, *args, **kwargs)
而不是编写自己的代码来返回响应。
Menu File