我有这个错误:
使用关键字参数'{'pk':''}'找不到'详细信息'。尝试了1种模式:['entry \ /(?P [0-9] +)$']
第7行:
来自_calender_entry.html
<div class="calender_entry col-lg-4">
<h2> {{ entry.name }} </h2>
<h4> {{ entry.date }} </h4>
<p>
{{ entry.description }}
</p>
<button onclick="window.location='{% url 'details' pk=entry.id %}'" class="btn btn-primary"> View Details </button>
</div>
这是来自我的 urls.py 文件:
urlpatterns = [
path('', views.index, name='index'),
path('entry/<int:pk>', views.details, name='details'),
path('admin/', admin.site.urls),
]
以下是来自 views.py 的方法:
def index(request):
entries = Entry.objects.all()
return render(request, 'myapp/index.html', {'entries' : entries})
def details(request, pk):
entry = Entry.objects.get(id=pk)
return render(request, 'myapp/details.html', {'entry' : entry})
这是 models.py
class Entry(models.Model):
name = models.CharField(max_length=100)
date = models.DateTimeField()
description = models.TextField()
created = models.DateTimeField( auto_now_add = True )
def __str__(self):
return f'{self.name} {self.date}'
_calender_entry.html包含在index.html文件中,如下所示:
{% block content %}
<div class="container">
<div class="row">
{% for entry in entries %}
{% include 'myapp/_calender_entry.html' %}
{% endfor %}
</div>
</div>
任何人都可以告诉我如何解决这个问题?
答案 0 :(得分:0)
现在django强迫你,每个应用程序都有自己的urls.py。
在myapp /目录中创建urls.py文件
AttributeError: 'NoneType' object has no attribute 'grid'
项目的urls.py必须如下
# myapp/urls.py
from django.urls import path
from myapp import views
urlpatterns = [
path('', views.index, name='index'),
path('entry/<int:pk>/', views.details, name='details'),
]
在模板中使用如下
# calenderproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(('myapp.urls', 'myapp'), namespace='myapp')),
]
它是如何工作的,我已经检查了
我发现了错误,请从模板_calender_entry.html
中删除此代码# myapp/_calender_entry.html
<a href="{% url 'myapp:details' pk=entry.id %}" class="btn btn-primary"> View Details </a>
虽然这与html django相同,但它执行它以在模板中注释使用<!-- {% include 'myapp/_calender_entry.html' %} -->
这样django不会执行它