Reputation: 663
As the title says, I am having difficulty making UpdateView work. When I press save the form doesn't save and I get a 404 error with the message No User matches the given query. I am having a really tough time trying to get this work and have spent hours looking at examples online but still can't work out where I am going wrong! If you could shine some light on this it would be appreciated!
Just to clarify I want my user to be able to edit their project, and only that user can edit it. I suspect I could be doing multiple things wrong!
urls
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'),
)
views
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})
models
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 template
{% 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 %}
update_project.html template
{% 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 %}
Upvotes: 2
Views: 723
Reputation: 308789
Your form action is /update_project/
, but this doesn't match your url pattern for updating projects. Instead, the url matches the views.profile_page
view, which treats update_project
as a username. This raises a 404, since you don't have a user with that username.
If you remove the form action
attribute, the form will submit to the current page, which I believe is what you want.
Alternatively you can change the action to
action="{% url 'update_project' project_user.username project.slug %}"
If you do this, you'll have to make sure that the variables are in the template context.
Upvotes: 1