UpdateView 不会保存表单,按保存后出现 404 错误
UpdateView won't save form and I get 404 error after I press save
正如标题所说,我很难使 UpdateView 正常工作。当我按下保存时,表单没有保存,我收到 404 错误消息,消息没有用户匹配给定的查询。我在尝试完成这项工作时遇到了很多困难,并且花了数小时在线查看示例,但仍然无法弄清楚我哪里出错了!如果您能对此有所启发,我们将不胜感激!
澄清一下,我希望我的用户能够编辑他们的项目,并且只有该用户可以编辑它。我怀疑我可能做错了很多事!
网址
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register_profile/$', views.register_profile, name='register_profile'),
url(r'^update_profile/$', views.update_profile, name='update_profile'),
url(r'^create_project/$', login_required(views.CreateProject.as_view()), name='create_project'),
url(r'^update_project/(?P<username>\w+)/(?P<slug>[-\w]+)/$', login_required(views.UpdateProject.as_view()), name='update_project'),
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
)
观看次数
class UpdateProject(UpdateView):
model = UserProject
form_class = UserProjectForm
template_name = 'howdidu/update_project.html'
def get_object(self, *args, **kwargs):
obj = super(UpdateProject, self).get_object(*args, **kwargs)
if obj.user != self.request.user:
raise PermissionDenied() #or Http404
return obj
def form_valid(self, form):
userproject = form.save(commit=False)
form.instance.user = self.request.user
userproject.save()
self.object = userproject
return super(UpdateProject, self).form_valid(form)
def get_success_url(self):
project_username = self.request.user.username
project_slug = self.object.slug
return reverse('user_project', kwargs={'username':project_username, 'slug': project_slug})
型号
class UserProject(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
project_overview = models.CharField(max_length=1000)
project_picture = models.ImageField(upload_to='project_images', blank=True)
date_created = models.DateTimeField(auto_now_add=True)
project_views = models.IntegerField(default=0)
project_likes = models.IntegerField(default=0)
project_followers = models.IntegerField(default=0)
slug = models.SlugField(max_length=100, unique=True) #should this be unique or not?
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(UserProject, self).save(*args, **kwargs)
def __unicode__(self):
return self.title
project.html 模板
{% extends 'howdidu/base.html' %}
{% load staticfiles %}
{% block title %}Project{% endblock %}
{% block body_block %}
{% if project %}
<h1>{{ project.title }}</h1>
<img src="{{ project.project_picture.url }}" width = "300" height = "300" />
<h3>{{ project.project_overview }}</h3>
<p><a href="{% url 'update_project' project_user.username project.slug %}">Edit project</a></p>
{% else %}
The specified project {{ project.title }} does not exist!
{% endif %}
{% endblock %}
更新_project.html 模板
{% extends 'howdidu/base.html' %}
{% load staticfiles %}
{% block title %}Update project{% endblock %}
{% block body_block %}
<h1>Edit your project</h1>
<form enctype="multipart/form-data" id="UserProjectForm" method="post" action="/update_project/">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="submit" value="Save" />
</form>
{% endblock %}
您的表单操作是 /update_project/
,但这与您更新项目的 url 模式不匹配。相反,url 匹配 views.profile_page
视图,后者将 update_project
视为用户名。这会引发 404,因为您没有使用该用户名的用户。
如果删除表单action
属性,表单将提交到当前页面,我相信这就是您想要的。
或者您可以将操作更改为
action="{% url 'update_project' project_user.username project.slug %}"
如果这样做,您必须确保变量在模板上下文中。
正如标题所说,我很难使 UpdateView 正常工作。当我按下保存时,表单没有保存,我收到 404 错误消息,消息没有用户匹配给定的查询。我在尝试完成这项工作时遇到了很多困难,并且花了数小时在线查看示例,但仍然无法弄清楚我哪里出错了!如果您能对此有所启发,我们将不胜感激!
澄清一下,我希望我的用户能够编辑他们的项目,并且只有该用户可以编辑它。我怀疑我可能做错了很多事!
网址
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register_profile/$', views.register_profile, name='register_profile'),
url(r'^update_profile/$', views.update_profile, name='update_profile'),
url(r'^create_project/$', login_required(views.CreateProject.as_view()), name='create_project'),
url(r'^update_project/(?P<username>\w+)/(?P<slug>[-\w]+)/$', login_required(views.UpdateProject.as_view()), name='update_project'),
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
)
观看次数
class UpdateProject(UpdateView):
model = UserProject
form_class = UserProjectForm
template_name = 'howdidu/update_project.html'
def get_object(self, *args, **kwargs):
obj = super(UpdateProject, self).get_object(*args, **kwargs)
if obj.user != self.request.user:
raise PermissionDenied() #or Http404
return obj
def form_valid(self, form):
userproject = form.save(commit=False)
form.instance.user = self.request.user
userproject.save()
self.object = userproject
return super(UpdateProject, self).form_valid(form)
def get_success_url(self):
project_username = self.request.user.username
project_slug = self.object.slug
return reverse('user_project', kwargs={'username':project_username, 'slug': project_slug})
型号
class UserProject(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
project_overview = models.CharField(max_length=1000)
project_picture = models.ImageField(upload_to='project_images', blank=True)
date_created = models.DateTimeField(auto_now_add=True)
project_views = models.IntegerField(default=0)
project_likes = models.IntegerField(default=0)
project_followers = models.IntegerField(default=0)
slug = models.SlugField(max_length=100, unique=True) #should this be unique or not?
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(UserProject, self).save(*args, **kwargs)
def __unicode__(self):
return self.title
project.html 模板
{% extends 'howdidu/base.html' %}
{% load staticfiles %}
{% block title %}Project{% endblock %}
{% block body_block %}
{% if project %}
<h1>{{ project.title }}</h1>
<img src="{{ project.project_picture.url }}" width = "300" height = "300" />
<h3>{{ project.project_overview }}</h3>
<p><a href="{% url 'update_project' project_user.username project.slug %}">Edit project</a></p>
{% else %}
The specified project {{ project.title }} does not exist!
{% endif %}
{% endblock %}
更新_project.html 模板
{% extends 'howdidu/base.html' %}
{% load staticfiles %}
{% block title %}Update project{% endblock %}
{% block body_block %}
<h1>Edit your project</h1>
<form enctype="multipart/form-data" id="UserProjectForm" method="post" action="/update_project/">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="submit" value="Save" />
</form>
{% endblock %}
您的表单操作是 /update_project/
,但这与您更新项目的 url 模式不匹配。相反,url 匹配 views.profile_page
视图,后者将 update_project
视为用户名。这会引发 404,因为您没有使用该用户名的用户。
如果删除表单action
属性,表单将提交到当前页面,我相信这就是您想要的。
或者您可以将操作更改为
action="{% url 'update_project' project_user.username project.slug %}"
如果这样做,您必须确保变量在模板上下文中。