如何设置答案将与之相关的问题?

艾哈迈德·麦加

我正在建立一个用于练习的问答网站,我希望每个答案都与一个作者和一个问题相关联,我设法将其与用户相关联,但是我无法弄清问题部分。

这是代码:

models.py:

class Questiont(models.Model):
    question = models.CharField(max_length=200)
    description = models.TextField(null = True , blank=True)
    date_posted =models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        return self.question

class Answer(models.Model):
    content = models.TextField(null=True,blank=True)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    author = models.ForeignKey(User,on_delete=models.CASCADE)
    date_posted = models.DateTimeField(default=timezone.now)

views.py(关联发生的位置):

class CreateAnswer(LoginRequiredMixin,CreateView):
    model = Answer
    fields = ['content']
    context_object_name = 'answer'
    success_url = reverse_lazy('Lisk home')


    def form_valid(self, form):
        form.instance.question = ?????????
        form.instance.author = self.request.user
        return super().form_valid(form)

我如何form.instance.question指定要指定答案的问题()?授予要回答的问题的ID在回答页(模板)的URL中。

网址是这样的:

http://127.0.0.1:8000/question/22/createanswer/

urls.py(不是root):

politicspost=questioon (sorry if this is messy)

 path('politics_topic/', views.Politics_topic.as_view(template_name='lisk_templates/politics_topic_template.html'),
         name='Politics_topic'),
    path('ask_politics/', views.Questionpolitics.as_view(template_name='lisk_templates/ask_politics_template.html'),
         name='ask_politics'),
    path('politicspost/<int:pk>/',views.Politics_post_details.as_view(template_name='lisk_templates/politics_post_details.html'),
         name='politics_post_details'),
    path('politicspost/<int:pk>/update/',views.Updatepolitics.as_view(template_name='lisk_templates/ask_politics_template.html'),
         name='updatepoliticspost'),
    path('politicspost/<int:pk>/delete/',views.Deletepoliticspost.as_view(template_name='lisk_templates/delete_politics_confirmation.html'),name ='deletepoliticspost'),

    #ANSWER
    path('politicspost/<int:id>/createanswer/',views.CreateAnswer.as_view(template_name='lisk_templates/createanswer.html'),name = 'createanswer'),
    path('answers/',views.Answerslist.as_view(template_name='lisk_templates/politics_post_details.html'),name ='answers')

提前致谢。

梅尔文

当您在一个视图/ URL路径中处理多个对象时,使用描述性名称总是很方便的:使用question_idnot id,这样它就不同于answer_id,稍后可能需要使用/politics_post/<int:question_id>/answers/<int:answer_id>/change/,例如。

这些名称只是标签。它们保存在kwargsCreateView属性中。因此/politics_post/42/answers/59/change将创建:CreateView.kwargs = {'question_id': 42, 'answer_id': 59}文档中有更多信息

正如@ crimsonpython24所说,您可以从问题的角度进行操作,但是使用Answer作为模型也可以。现在,我将假设您将URL更改为具有question_id。您将像这样更改您的创建视图:

class CreateAnswer(LoginRequiredMixin,CreateView):
    model = Answer
    fields = ['content']
    context_object_name = 'answer'
    success_url = reverse_lazy('Lisk home')
    question_kwarg = 'question_id'


    def form_valid(self, form):
        try:
            question = Question.objects.get(pk=self.kwargs[self.question_kwarg])
        except Question.DoesNotExist:
            form.add_error(None, 'Invalid question')
            return self.form_invalid(form)
        form.instance.question = question
        form.instance.author = self.request.user
        return super().form_valid(form)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用AppScript设置问题的正确答案?

如何在Mac OS High Sierra上将与Ruby相关的任何内容重置为出厂设置

如何将与部署相关的参数传递给Webapp

如何将与SQLAlchemy相关的代码拆分为模块

如何检查JavaScript问题的答案?

如何使用 NA 按钮为每个问题设置灰色问题的条件(在 HTML 中有单选按钮答案的问题)?

如何使用ajax将与每个按钮相关的数据发送到php?

如何将与计算工作时间相关的查询转换为MS Access

如何将与文章ID相关联的图片ID链接到图库中?

如何将与wordpress主题相关的其他帖子更改为随机?

如何在 Hive SQL 中将与时间相关的事件组合在一起

如何使用jQuery将与类名称相关的样式添加到元素?

如何处理问题的答案?

如何存储密码重置问题的答案?

如何使用Flexbox创建问题/答案列?

如何在此Codeigniter 3应用程序中将与图像上传相关的验证错误存储为闪光信息?

如何使用存储过程和事务将与其他表相关的数据插入到表中?

如何修复与NFS问题相关的错误?

如何使用 HATEOAS 设置相关对象?

如何设置返回类型并显示相关toaster

如何设置相关实体(对象)的值

如何在StackOverflow上测试Django问题的答案

如何从 Google 表单的下拉问题中检索选定的答案

如何在Django班级视图中显示显示问题答案?

如何设计可以引用问题表或答案表的评论表?

如何强制所有 cpan 问题的默认答案?

如何通过Google表单审查问题的答案?

如何在测验中为checkboxGridItem问题分配正确答案?

设置图表类型xlDoughnut时与图表类型相关的问题