Django 问题 URL Mapping/DetailView
Issue with Django URL Mapping/DetailView
我是Django的新手,一直在做一个示例项目。我一直在尝试使用 Generic Detailview。 url 重定向似乎工作正常,但 DetailView 无法从 url.
获取主键
主要url.py::
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^',include('personal.urls')),
]
这是我的应用程序的 urls.py 代码:
urlpatterns = [
url(r'(?P<pk>\d+)/$',views.detailView.as_view(),name="detail"),]
DetailView 的查看文件:
from django.shortcuts import render
from django.views import generic
from .models import Story
class detailView(generic.DetailView):
model = Story
template_name = 'personal/storydetail.html'
def get_context_data(self, **kwargs):
pk = kwargs.get('pk') # this is the primary key from your URL
print("PK:",pk)
模板代码:
{% block content %}
{{ Story.writer }}
<h6> on {{ Story.story_title }}</h6>
<div class = "container">
{{ Story.collection }}
</div>
{% endblock %}
故事Class代码:
class Story(models.Model):
story_title = models.CharField(max_length=200) #Story title
writer = models.CharField(max_length=200) #WriterName
collection=models.CharField(max_length=200) #Collection/Book name
当我在视图中检查主键值时,它会显示 'NONE'。我找不到代码的问题。我的 pased url 看起来像:http://127.0.0.1:8000/personal/2/ 其中 personal 是应用程序的名称,2 应该作为 id。
问题是您在 get_context_data
方法中使用 kwargs
而不是 self.kwargs
。它应该是这样的:
def get_context_data(self, **kwargs):
# You need to call super() here, so that the context from the DetailView is included
kwargs = super(detailView, self).get_context_data(**kwargs)
pk = self.kwargs['pk'] # No need for get() here -- if you get a KeyError then you have a problem in your URL config that should be fixe # this is the primary key from your URL
# edit kwargs as necessary
...
return kwargs
在get_context_data
方法中,kwargs
是传递给该方法以构成上下文的那些。它们不同于 self.kwargs
,后者来自 url 模式。
我是Django的新手,一直在做一个示例项目。我一直在尝试使用 Generic Detailview。 url 重定向似乎工作正常,但 DetailView 无法从 url.
获取主键主要url.py::
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^',include('personal.urls')),
]
这是我的应用程序的 urls.py 代码:
urlpatterns = [
url(r'(?P<pk>\d+)/$',views.detailView.as_view(),name="detail"),]
DetailView 的查看文件:
from django.shortcuts import render
from django.views import generic
from .models import Story
class detailView(generic.DetailView):
model = Story
template_name = 'personal/storydetail.html'
def get_context_data(self, **kwargs):
pk = kwargs.get('pk') # this is the primary key from your URL
print("PK:",pk)
模板代码:
{% block content %}
{{ Story.writer }}
<h6> on {{ Story.story_title }}</h6>
<div class = "container">
{{ Story.collection }}
</div>
{% endblock %}
故事Class代码:
class Story(models.Model):
story_title = models.CharField(max_length=200) #Story title
writer = models.CharField(max_length=200) #WriterName
collection=models.CharField(max_length=200) #Collection/Book name
当我在视图中检查主键值时,它会显示 'NONE'。我找不到代码的问题。我的 pased url 看起来像:http://127.0.0.1:8000/personal/2/ 其中 personal 是应用程序的名称,2 应该作为 id。
问题是您在 get_context_data
方法中使用 kwargs
而不是 self.kwargs
。它应该是这样的:
def get_context_data(self, **kwargs):
# You need to call super() here, so that the context from the DetailView is included
kwargs = super(detailView, self).get_context_data(**kwargs)
pk = self.kwargs['pk'] # No need for get() here -- if you get a KeyError then you have a problem in your URL config that should be fixe # this is the primary key from your URL
# edit kwargs as necessary
...
return kwargs
在get_context_data
方法中,kwargs
是传递给该方法以构成上下文的那些。它们不同于 self.kwargs
,后者来自 url 模式。