Python 我应该如何解决Django中这个奇怪的主键错误?
我有一个Django待办事项列表应用程序。在该应用程序中,有一个页面提供了有关任务的所有详细信息,如子任务和注释等。该页面的url类似于“/todo/task name”。出于安全原因,我希望通过任务的pk。此外,如果我在数据库中检查一个任务及其pk和标题,那么这将允许用户拥有多个同名任务。但是,一旦我通过了带有URL的pk,我就会面临子任务和注释的奇怪问题。这是处理任务页面的my view函数:Python 我应该如何解决Django中这个奇怪的主键错误?,python,html,django,Python,Html,Django,我有一个Django待办事项列表应用程序。在该应用程序中,有一个页面提供了有关任务的所有详细信息,如子任务和注释等。该页面的url类似于“/todo/task name”。出于安全原因,我希望通过任务的pk。此外,如果我在数据库中检查一个任务及其pk和标题,那么这将允许用户拥有多个同名任务。但是,一旦我通过了带有URL的pk,我就会面临子任务和注释的奇怪问题。这是处理任务页面的my view函数: def todo_detail(request, title): try:
def todo_detail(request, title):
try:
todo = ToDo.objects.get(title=title, creator=request.user)
except:
return render(request, "ToDo/restrict_access.html")
subtasks = SubTask.objects.filter(parent_task=todo)
try:
note = Notes.objects.get(parent_task=todo)
except:
note = Notes()
if request.method == "POST":
note_form = ToDoNotesForm(request.POST)
subtask_form = SubTaskForm(request.POST)
due_form = DueDateForm(request.POST)
if note_form.is_valid():
task_notes = note_form.cleaned_data.get("task_notes")
new_note = Notes(content=task_notes)
new_note.parent_task = todo
new_note.save()
todo.has_notes = True
todo.save()
messages.success(request, "Your notes are saved")
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
elif subtask_form.is_valid():
subtask_title = subtask_form.cleaned_data.get("sub_task")
subtask = SubTask(title=subtask_title)
subtask.parent_task = todo
subtask.parent_task.num_of_subtasks += 1
subtask.parent_task.save()
subtask.save()
messages.success(request, "Subtask added")
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
elif due_form.is_valid():
days = due_form.cleaned_data.get("due_date").lower()
if days == "today":
days = 0
elif days == "tomorrow":
days = 1
elif days == "next week":
days = 7
elif days == "yesterday":
days = -1
elif days == "last week":
days = -7
else:
days = int(days)
today = datetime.datetime.today()
due_date = today + datetime.timedelta(days=days)
todo.due_date = due_date
todo.save()
messages.success(request, "Due Date added to task")
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
else:
note_form = ToDoNotesForm()
subtask_form = SubTaskForm()
due_form = DueDateForm()
context = {
"todo": todo,
"note_form": note_form,
"note": note,
"subtask_form": subtask_form,
"subtasks": subtasks,
"due_form": due_form,
"title": todo.title,
"percentage": percentage
}
return render(request, "ToDo/detailed_view.html", context=context)
在“详细信息”页面中,有用于选中、取消选中或编辑子任务或编辑注释的链接。在html页面中,我确保只通过编辑或检查视图功能的相应pk
There were some HTML code here that is not relevent
{% if subtasks %}
<br><br>
<center style="font-size:27px; background-color: burlywood; color: 'black';">Subtasks for this task:</center>
<br>
<div style="padding-left: 4ch;">
{% for subtask in subtasks %}
<div class="content-section dark-mode-assist-section">
{% if not subtask.done %}
{% if request.user_agent.is_mobile %}
<li style="font-size: 23px;">
<a style="color:inherit; text-decoration: none;" href="{% url 'todo-edit-subtask' subtask.pk %}"> {{ subtask.title }} </a>
<br>
<button onclick="location.href='{% url 'todo-toggle-subtask' subtask.pk %}'" type="button" class="btn btn-outline-success">Check it off</button>
<button onclick="location.href='{% url 'delete-item' 'subtask' subtask.pk %}'" type="submit" class="btn btn-outline-danger">Delete</button>
</li>
{% else %}
<li style="font-size: 23px;">
<a style="color:inherit; text-decoration: none;" href="{% url 'todo-edit-subtask' subtask.pk %}"> {{ subtask.title }} </a>
<button onclick="location.href='{% url 'todo-toggle-subtask' subtask.pk %}'" style="float: right; size:3ch" type="button" class="btn btn-outline-success">Check it off</button>
<button onclick="location.href='{% url 'delete-item' 'subtask' subtask.pk %}'" style="float: right; size:3ch" type="submit" class="btn btn-outline-danger">Delete</button>
</li>
{% endif %}
{% else %}
{% if request.user_agent.is_mobile %}
<li style="font-size: 23px;">
<a style="color:inherit; text-decoration: line-through;" href="{% url 'todo-edit-subtask' subtask.pk %}"> {{ subtask.title }} </a>
<br>
<button onclick="location.href='{% url 'todo-toggle-subtask' subtask.pk %}'" type="button" class="btn btn-outline-success">Uncheck it </button>
<button onclick="location.href='{% url 'delete-item' 'subtask' subtask.pk %}'" type="submit" class="btn btn-outline-danger">Delete</button>
</li>
{% else %}
<li style="font-size: 23px;"><a class="control-subtask" style="color:inherit; text-decoration: line-through;" href="{% url 'todo-edit-subtask' subtask.pk %}"> {{ subtask.title }} </a> <button onclick="location.href='{% url 'todo-toggle-subtask' subtask.pk %}'" style="float: right; size:3ch" type="button" class="btn btn-outline-success">Uncheck it</button> <button onclick="location.href='{% url 'delete-item' 'subtask' subtask.pk %}'" style="float: right; size:3ch" type="submit" class="btn btn-outline-danger">Delete</button> </li>
{% endif %}
{% endif %}
</div>
{% endfor %}
{% if percentage != 0 %}
<center style="font-size:23px; background-color: burlywood; color: 'black';">Your progress:</center>
<center> <span style="font-size:10ch; color:lightseagreen">{{ percentage }}%</span></center>
{% endif %}
</div>
{% endif %}
{% if todo.has_notes %}
<br>
<center style="font-size:27px; background-color: burlywood; color: 'black';">Notes for this task:</center>
<h3>
<br>
<div style="font-family:Candara">{{ note.content|linebreaks }}</div>
<button class="btn btn-outline-danger" type="submit" onclick="location.href='{% url 'delete-item' 'notes' note.pk %}'">Delete notes</button>
<button class="btn btn-outline-info" type="submit" onclick="location.href='{% url 'todo-edit-notes' note.pk %}'">Edit notes</button>
</h3>
<p>
<br>
Notes added on: <b>{{ note.date_added|date:"F d, Y" }}</b>
<br>
{% if note.date_edited != note.date_added %}
Notes edited on: <b>{{ note.date_edited|date:"F d, Y" }}</b>
{% endif %}
</p>
{% else %}
<br>
<form method="POST" name={{ note.pk }} >
{% csrf_token %}
<fieldset class="form-group dark-mode-assist">
<legend class="border-bottom mb-4">Add notes</legend>
{{ note_form|crispy }}
<input type="hidden" name="title" value={{ note.pk }}>
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Add</button>
</div>
</form>
{% endif %}
</div>
{% endblock content %}
所以很明显,如果没有找到待办事项,我应该在访问详细信息页面时首先出错。但我可以进入详细信息页面。问题是当我想检查子任务时,他们说找不到ToDo,但实际上我正在检查子任务not ToDo。因为第一个查询与数据库不匹配。你犯了这个错误。因此,您可以使用
ToDo.DoesNotExist
跟踪异常,如下所示:
try:
todo = ToDo.objects.get(title=title, creator=request.user)
except ToDo.DoesNotExist:
return render(request, "ToDo/restrict_access.html")
您的查看功能
toggle\u子任务
没有路由到,因为todo\u detail
正在路由到第一个
从中的django文档:
路径信息匹配
urls.py
文件中,您可以对URL进行结构化,使最具体的URL(如/todo/toggle_subtask/
)位于最不具体的URL(如/todo/
)之前:
从django.url导入路径
从…起导入视图#无论从何处导入'views.py'
URL模式=[
#/todo/toggle\u子任务//
路径('/todo/toggle_子任务//',views.toggle_子任务),
#/待办事项//
路径('/todo/',视图.todo_详细信息),
]
我收到一个错误,因为DoesNotExist不是ToDo的属性。第二,我想把问题弄清楚。我已使用“切换子任务”功能更新了问题,以帮助您更好地理解。请检查一下。请包括相关的url.py
你是绝对正确的人!!我正要把我的URL.py包括进去,我看到你已经回答了。在我的url.py中,我看到“/todo/”位于“/todo/toggle_subtask/”之前。我已经改变了他们的命令,现在一切都很顺利。再次感谢您,说实话,我今天学到了一些关于Django的新知识,我相信这对我将来也会有很大的帮助。:)
try:
todo = ToDo.objects.get(title=title, creator=request.user)
except ToDo.DoesNotExist:
return render(request, "ToDo/restrict_access.html")