我正在努力扩展官方django教程的民意调查应用程序。我正在创建配置文件功能,用户可以在其中注册和编辑配置文件信息。
表单正确呈现,但当我实际提交信息以更新配置文件信息时,我收到错误请求(400)错误。
这是我在控制台中看到的:
[02/Feb/2015 12:02:42] "GET /polls/edit_profile/ HTTP/1.1" 200 3394
[02/Feb/2015 12:02:55] "POST /polls/edit_profile/ HTTP/1.1" 400 26
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
first_name = models.CharField(max_length=100, blank=True, null=True)
last_name = models.CharField(max_length=100, blank=True, null=True)
birth_date = models.DateField(blank=True, null=True)
location = models.CharField(max_length=120)
def __unicode__(self):
return unicode(self.user)
主/ urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from registration.backends.simple.views import RegistrationView
class MyRegistrationView(RegistrationView):
def get_success_url(selfself, request, user):
return '/polls/'
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
(r'^accounts/', include('registration.backends.simple.urls')),
)
轮询/ urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^add_poll/$', views.add_poll, name='add_poll'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^profile_page/$', views.ProfileView.as_view(), name='profile_page'),
url(r'^edit_profile/$', views.edit_profile, name='edit_profile'),
)
forms.py
class UserProfileForm(forms.ModelForm):
birth_date = forms.DateField()
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'birth_date',)
forms.DateInput.input_type="date"
forms.DateTimeInput.input_type="datetime-local"
views.py
class ProfileView(generic.TemplateView):
model = UserProfile
template_name = 'polls/profile_page.html'
def edit_profile(request):
if request.method == 'POST':
form = UserProfileForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('polls:profile_page')
else:
form = UserProfileForm()
return render(request, 'polls/edit_profile.html', {'form': form})
edit_profile.html
{% extends 'polls/base.html' %}
{% block title %}Edit Profile{% endblock %}
{% block body_block %}
<h3>Current Name: {{ user.userprofile.first_name }} {{ user.userprofile.last_name }}</h3>
<h3>Birth-Date: {{ user.userprofile.birth_date }}
<br><br/>
<form action="/polls/edit_profile/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
我的观点几乎是Django文档中的基本处理,我想知道我是否在使用urls.py搞乱某事,或者我的edit_html的动作变量是不正确的?
非常欢迎有关改进我所提出的建议的想法和建议。我想我已经提供了所有相关信息,但是如果我还能提供其他信息,请告诉我。
干杯
答案 0 :(得分:4)
我认为你错误地使用了HttpResponseRedirect
。
应该是:
return HttpResponseRedirect('profile_page/')
我认为它不会呈现网址名称(https://docs.djangoproject.com/en/1.7/ref/request-response/#django.http.HttpResponseRedirect)
如果您希望能够通过网址名称重定向(这是正确的方法),请使用快捷方式redirect
from django.shortcuts import redirect
def edit_profile(request):
if request.method == 'POST':
form = UserProfileForm(request.POST)
if form.is_valid():
return redirect('polls:profile_page')
请参阅此处的文档:https://docs.djangoproject.com/en/1.7/topics/http/shortcuts/#redirect