我正在尝试允许用户使用UpdateView编辑输入到项目中的信息。我一直收到错误说"通用详细信息视图必须使用对象pk或slug调用UpdateProject。"我已尝试指向UpdateView类中的slug,但无法使其工作。我是Django的新手,所以任何帮助都会非常感激!
视图
class CreateProject(CreateView):
model = UserProject
form_class = UserProjectForm
template_name = 'howdidu/create_project.html'
def form_valid(self, form):
userproject = form.save(commit=False)
form.instance.user = self.request.user
userproject.save()
self.object = userproject
return super(CreateProject, 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 UpdateProject(UpdateView):
model = UserProject
form_class = UserProjectForm
template_name = 'howdidu/update_project.html'
slug_field = 'slug'
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})
网址
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/$', 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 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
答案 0 :(得分:1)
Django的UpdateView默认需要来自相应网址的pk或slug参数。在您的情况下,您需要将URL更改为UpdateProject视图
url(r'^update_project/(?P<slug>[-\w]+)/$',
login_required(views.UpdateProject.as_view()),
name='update_project'),
slug是你的项目的slu ..
答案 1 :(得分:0)
在您的应用中创建utils.py文件。使用django utils中的slugify。
import random
import string
from django.utils.text import slugify
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return "".join(random.choice(chars) for _ in range(size))
def unique_slug_generator(instance, new_slug=None):
if new_slug is None:
slug = new_slug
else:
slug=slugify(instance.model field of your choice)
Klass = instance.__class__
qs_exists = Klass.objects.filter(slug=slug).exists()
if qs_exists:
new_slug = "{slug}-{randstr}".format(
slug=slug,
randstr=random_string_generator(size=4)
)
return unique_slug_generator(instance, new_slug=new_slug)
return slug
向模型添加块
slug = models.SlugField(null=True, blank=True)
将其添加到您的urls.py文件中
url(r'^update/(?P<slug>[\w-]+)/$', profileUpdate.as_view(), name='update'),
答案 2 :(得分:0)
class UserProject(UpdateView):
model = BlogSlider
fields = ['title', 'sub_title', 'description', 'image']
template_name = 'update.html'
使用表格并使用{{form.as_p}}。