Python NoReverseMatch at/post/1/log/Reverse for';日志创建';带有关键字参数';{';邮政id';:';';}';找不到
我有一个帖子模型,里面有一大堆帖子。我还有一个日志模型,它在Post模型中有一个外键字段。基本上,日志模型在Post模型中存储Post的日志条目(基本上是Post注释)。一切都很顺利。我一直在为我的帖子模型使用CBV,我使用CBV列出我的日志条目。然后,我添加了一个链接,使用以下锚标记将我重定向到日志CreateView:Python NoReverseMatch at/post/1/log/Reverse for';日志创建';带有关键字参数';{';邮政id';:';';}';找不到,python,django,django-urls,django-class-based-views,Python,Django,Django Urls,Django Class Based Views,我有一个帖子模型,里面有一大堆帖子。我还有一个日志模型,它在Post模型中有一个外键字段。基本上,日志模型在Post模型中存储Post的日志条目(基本上是Post注释)。一切都很顺利。我一直在为我的帖子模型使用CBV,我使用CBV列出我的日志条目。然后,我添加了一个链接,使用以下锚标记将我重定向到日志CreateView: <a class="btn" href="{% url 'log-create' post_id=logs.post_id %}">Add Entry</a
<a class="btn" href="{% url 'log-create' post_id=logs.post_id %}">Add Entry</a>
我的看法:
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView, CreateView
from .models import Post, Log
from django.http import HttpResponseRedirect
from django.contrib.auth.mixins import LoginRequiredMixin
class LogListView(ListView):
model = Log
template_name = 'blog/log_entries.html'
context_object_name = 'logs'
ordering = ['-date_posted']
def get_queryset(self):
self.post = get_object_or_404(Post, log=self.kwargs['pk'])
return Log.objects.filter(post=self.post)
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(LogListView, self).get_context_data(**kwargs)
# Add in a QuerySet of all images related to post
context['post'] = Post.objects.all()
return context
class LogCreateView(LoginRequiredMixin, CreateView):
model = Log
fields = [
'log_entry'
]
def form_valid(self, form):
form.instance.log_author = self.request.user
post = Post.objects.get(pk=self.kwargs['post_id'])
return super().form_valid(form)
我的URL.py
from django.urls import path, include
from . import views
from .views import LogListView, LogCreateView
urlpatterns = [
path('', PostListView.as_view(), name='blog-home'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('post/<int:pk>/log/', LogListView.as_view(), name='log-list'),
path('post/<int:post_id>/log/new/', LogCreateView.as_view(), name='log-create'),
]
从django.url导入路径,包括
从…起导入视图
从.views导入LogListView、LogCreateView
URL模式=[
路径(“”,PostListView.as_view(),name='blog-home'),
路径('post/',PostDetailView.as_view(),name='post-detail'),
路径('post/new/',PostCreateView.as_view(),name='post-create'),
路径('post//log/',LogListView.as_view(),name='log-list'),
路径('post//log/new/',LogCreateView.as_view(),name='log-create'),
]
最后,我的模板:
{% extends "blog/base.html"%}
{% block body_class %} home-section {% endblock %}
{% block content %}
<div class="container">
<h2>Log Entries</h2>
{% for log in logs %}
<div class="row">
<article class="content-section">
<div class="article-metadata log-metadata">
<a class="mr-2" href="{% url 'profile' user=log.log_author %}">{{ log.log_author }}</a>
<small class="text-muted">{{ log.date_posted|date:"d F Y" }}</small>
{% if request.user.is_authenticated and request.user == log.log_author %}
<a href="#"><ion-icon name="trash"></ion-icon></a>
{% endif %}
</div>
<p class="">{{ log.log_entry }}</p>
</article>
</div>
{% endfor %}
<a class="btn" href="{% url 'log-create' post_id=logs.post_id %}">Add Entry</a>
</div>
{% endblock content %}
{%extends“blog/base.html”%}
{%block body_class%}主节{%endblock%}
{%block content%}
日志条目
{%用于登录日志%}
{{log.date_posted|date:“dfy”}
{%if request.user.u经过身份验证且request.user==log.log\u author%}
{%endif%}
{{log.log_entry}
{%endfor%}
{%endblock内容%}
我认为我正确地向url传递了一个参数。当我将post_id=1时,这一点很明显。但我不确定我的称呼是否正确。在这个问题上的任何帮助都将非常感谢
更新:我将LogListView中的上下文\对象\名称编辑为日志,以减少for循环的混乱。基本上,我正在尝试在所有日志条目的底部获取一个锚定标记,以重定向到添加条目页面。我建议使用第一个元素,仅在存在可用对象时呈现链接:
</article>
</div>
{% if forloop.first %}<a class="btn" href="{% url 'log-create' post_id=log.post.id %}">Add Entry</a>{% endif %}
{% endfor %}
{%if-forloop.first%}{%endif%}
{%endfor%}
这log.post.id
表示获取日志对象的post对象(外键)的id
</article>
</div>
{% if forloop.first %}<a class="btn" href="{% url 'log-create' post_id=log.post.id %}">Add Entry</a>{% endif %}
{% endfor %}