我有一个名为 account 的应用程序,其中包含用于注册和登录用户的所有模型、视图和表单。
我有一个位于 app 文件夹之外的模板,它假设包含帐户 app 中的所有表单。
我在尝试获取模板中显示的表单时遇到问题。
有人能帮我吗?
以下是一些代码片段:
帐户/forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
# Safe from injection, etc.
class UserRegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
first_name = forms.CharField(max_length=30, required=True)
last_name = forms.CharField(max_length=30, required=True)
class Meta:
model = User
fields = ('username','email','password1', 'password2','first_name','last_name')
def save(self, commit=True):
user = super(UserRegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
return user
帐户/view.py
from forms import UserRegistrationForm
def register_user(request):
if request.POST:
form = UserRegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
else:
form = UserRegistrationForm()
args = {}
# prevent forgery
args.update(csrf(request))
# empty form
args['form'] = form
return render_to_response('signup.html', args)
def register_success(request):
return render_to_response('signup_success.html')
最后是模板,它不在帐户文件夹中。它与 settings.py 位于同一文件夹中
注册.html
{% extends "base.html" %}
{% block content %}
<form action="" method="post"> {% csrf_token %}
<ul>
{{accounts.form.as_ul}}
</ul>
<input type="submit" name="submit" value="Register">
</form>
{% endblock %}
更新
从项目经理那里获得移动模板的权限后,我将其移动到帐户/模板,并将渲染更改为响应地址。我现在有表单不提交的新问题。天哪,这是怎么回事??