【发布时间】:2019-10-14 17:05:40
【问题描述】:
我有一个包含作业问题的页面。 与作业相关的问题显示在作业详细信息页面上,带有 EDIT 和 DELETE 锚标记。 但是在按下删除后我得到一个错误:Reverse for 'CreateQuestion' with arguments '('',)' not found。尝试了 1 种模式:['assignment/(?P[0-9]+)/createQuestion/$']
views.py
class AssignmentDelete(DeleteView):
model = Assignment
template_name = "dashboard/assignment_list.html"
success_url = reverse_lazy('ViewAssignment')
def get(self, request, *args, **kwargs):
return self.delete(request, *args, **kwargs)
class AssignmentDetailView(generic.DetailView):
model = Assignment
template_name = "dashboard/assignment_detail.html"
class QuestionDeleteView(DeleteView):
model = Question
template_name = 'dashboard/assignment_detail.html'
def get_success_url(self):
return reverse_lazy('assignment_detail', kwargs={'pk': self.object.assignment_id})
urls.py
path('<int:assignment_pk>/DeleteQuestion/<int:pk>/delete', views.QuestionDeleteView.as_view(), name='DeleteQuestion'),
path('<int:pk>/createQuestion/', views.QuestionCreate, name='CreateQuestion'),
path('assignment/<int:pk>', views.AssignmentDetailView.as_view(), name='assignment_detail'),
assignment_detail.html
{% extends "./base.html" %}
{% block content %}
<h1>Course:{{assignment.course }}</h1>
<p><strong>Assignment:</strong> {{ assignment.number }}</p>
<p><strong>publish_date:</strong> {{ assignment.publish_date }}</p>
<p><strong>deadline:</strong> {{assignment.deadline_date}}</p>
<div style="margin-left:20px;margin-top:20px">
<h4>Questions</h4>
<table class="table">
<thead class="thead-light">
<tr>
<th scope="col">Question Title</th>
<th scope="col">Question Marks</th>
<th scope="col">Edit Question</th>
<th scope="col">Delete Question</th>
</tr>
</thead>
<tbody>
{% for question in assignment.question_set.all %}
<tr>
<td>{{ question.title }}</td>
<td>{{ question.marks }}</td>
<td> <a href="{% url 'UpdateQuestion' assignment.id question.id %}">Edit</a> </td>
<td> <a onclick="return confirm('Are you sure?');" href="{% url 'DeleteQuestion' assignment_pk=assignment.id pk=question.id %}">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<a class=" btn btn-primary" href="{% url 'CreateQuestion' assignment.id %}">Add Question</a>
</div>
{% endblock %}
{% block script %}
{% endblock %}
模型.py
class Assignment(models.Model):
number = models.IntegerField()
course = models.ForeignKey(Course,on_delete = models.SET_NULL,blank=True,null=True)
publish_date = models.DateField(auto_now_add = True)
deadline_date = models.DateField()
faculty = models.ForeignKey(Faculty,on_delete = models.SET_NULL,blank=True,null=True)
def __str__(self):
return f'Assignment {self.number}-{self.course}'
def get_absolute_url(self):
"""Returns the url to access a detail record for this Assignment."""
return reverse('assignment-detail', args=[str(self.id)])
class Question(models.Model):
assignment = models.ForeignKey(Assignment, on_delete=models.SET_NULL,blank=True,null=True)
username = models.ForeignKey(User,on_delete=models.SET_NULL,blank=True,null=True)
title = models.CharField(max_length = 200)
description = models.TextField(blank=True , null = True)
marks = models.IntegerField(null=True)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
def get_absolute_url(self):
"""Returns the url to access a detail record for this book."""
return reverse('question-detail', args=[str(self.id)])
The Assignment detail page with questions associated with it
【问题讨论】:
-
您需要显示作业详细信息模板的其余部分,尤其是带有 createQuestion 链接的部分。
-
请检查我是否更新了作业详情模板
-
我不确定我是否理解您在做什么。为什么要使用
assignment_detail.html删除问题视图?该视图应使用特定模板,该模板以表单形式显示确认消息,该表单回发给自身以执行删除。 -
我已经添加了截图,请检查。请帮助我,我厌倦了在这背后浪费了一整天。 Question.ForeignKey = Assignment.ForeignKey=Coursehas2Foreingkey(Grade , Department) (这是我的模型与每个 pther 的关系)
-
我不知道这与我的问题有什么关系。我已经可以看到模板,但您没有回答为什么将 同一个模板 分配给 DeleteView 的问题。
标签: django