在 Django 中如何用当前的 detailview 对象初始化一个表单?
In Django how to initialize a form with the current detailview object?
我是建立博客网站的 Django 初学者。
这些模型是:博客、话题、评论。
博客有主题,每个主题都有评论。
登录的用户可以从线程详细信息视图创建带有表单的评论。
创建的 Comment 应该使用当前的 detailview thread 对象初始化其 'thread' 值,因为 thread 是 Comment 模型中 Thread 模型的外键。
在评论创建视图中,我尝试使用 form.instance.thread = self.kwargs['pk'] 但它带来了这个错误:/blogs/comment/create/'pk'处的按键错误。
你能帮我把话题传给表格吗?
models.py:
class Comment(models.Model):
comment_date = models.DateField(default=datetime.date.today, blank=True)
comment_text = models.TextField(max_length=1000, help_text='Enter your comment')
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
thread = models.ForeignKey('Thread', on_delete=models.SET_NULL, null=True)
class Meta:
verbose_name = 'comment'
verbose_name_plural = 'comments'
def __str__(self):
return f'{self.id} ({self.thread.thread_name})'
def get_absolute_url(self):
return reverse('comment-detail', args=[str(self.id)])
class Thread(models.Model):
thread_name = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
summary = models.TextField(max_length=1000,)
post_date = models.DateField(null=True, blank=True)
blog = models.ForeignKey('Blog', on_delete=models.CASCADE, null=True)
class Meta:
ordering = ['-post_date']
def __str__(self):
return self.thread_name
def get_absolute_url(self):
return reverse('thread-detail', args=[str(self.id)])
urls.py:
path('thread/<int:pk>/', views.ThreadDetailView.as_view(), name='thread-detail')
path('comment/create/', views.CommentCreate.as_view(), name='comment_create')
views.py:
class ThreadDetailView(generic.DetailView):
model = Thread
class CommentCreate(LoginRequiredMixin, CreateView):
model = Comment
fields = ['comment_text', 'thread']
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.thread = self.kwargs['pk'] # This doesn't work
return super(CommentCreate, self).form_valid(form)
我终于找到了另一种解决问题的方法:
通过在评论创建视图中初始化线程外键:
def CommentCreate(request, thread_id):
thread = Thread.objects.get(id=thread_id)
form = Commentform(request.POST or None, initial={'thread': thread})
....```
通过在调用创建视图的模板中传递线程 ID:
<a href="{% url 'comment_create' thread.pk %}"> Create a New Comment</a>
并且在视图的 url:
path('thread/<thread_id>/commentcreate/', views.CommentCreate, name='comment_create'),
我是建立博客网站的 Django 初学者。
这些模型是:博客、话题、评论。 博客有主题,每个主题都有评论。
登录的用户可以从线程详细信息视图创建带有表单的评论。 创建的 Comment 应该使用当前的 detailview thread 对象初始化其 'thread' 值,因为 thread 是 Comment 模型中 Thread 模型的外键。
在评论创建视图中,我尝试使用 form.instance.thread = self.kwargs['pk'] 但它带来了这个错误:/blogs/comment/create/'pk'处的按键错误。
你能帮我把话题传给表格吗?
models.py:
class Comment(models.Model):
comment_date = models.DateField(default=datetime.date.today, blank=True)
comment_text = models.TextField(max_length=1000, help_text='Enter your comment')
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
thread = models.ForeignKey('Thread', on_delete=models.SET_NULL, null=True)
class Meta:
verbose_name = 'comment'
verbose_name_plural = 'comments'
def __str__(self):
return f'{self.id} ({self.thread.thread_name})'
def get_absolute_url(self):
return reverse('comment-detail', args=[str(self.id)])
class Thread(models.Model):
thread_name = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
summary = models.TextField(max_length=1000,)
post_date = models.DateField(null=True, blank=True)
blog = models.ForeignKey('Blog', on_delete=models.CASCADE, null=True)
class Meta:
ordering = ['-post_date']
def __str__(self):
return self.thread_name
def get_absolute_url(self):
return reverse('thread-detail', args=[str(self.id)])
urls.py:
path('thread/<int:pk>/', views.ThreadDetailView.as_view(), name='thread-detail')
path('comment/create/', views.CommentCreate.as_view(), name='comment_create')
views.py:
class ThreadDetailView(generic.DetailView):
model = Thread
class CommentCreate(LoginRequiredMixin, CreateView):
model = Comment
fields = ['comment_text', 'thread']
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.thread = self.kwargs['pk'] # This doesn't work
return super(CommentCreate, self).form_valid(form)
我终于找到了另一种解决问题的方法:
通过在评论创建视图中初始化线程外键:
def CommentCreate(request, thread_id): thread = Thread.objects.get(id=thread_id) form = Commentform(request.POST or None, initial={'thread': thread}) ....```
通过在调用创建视图的模板中传递线程 ID:
<a href="{% url 'comment_create' thread.pk %}"> Create a New Comment</a>
并且在视图的 url:
path('thread/<thread_id>/commentcreate/', views.CommentCreate, name='comment_create'),