我正在尝试使用上下文将一些数据导入到我的HTML中 现在我的代码是以下,并且工作得很好。 我正在使用Try / Except,因为并不总是创建项目而且kwargs ['pk1'])并不总是存在:
我的工作代码:
class ProjectDetailView(generic.DetailView, LoginRequiredMixin):
#import pdb; pdb.set_trace()
model = Project
template_name = 'project_details.html'
def get_object(self, queryset=None):
return get_object_or_404(Project,id=self.kwargs['pk1'])
def get_context_data(self, **kwargs):
context = super(ProjectDetailView, self).get_context_data(**kwargs)
try:
team_name = Project.objects.get(id=self.kwargs['pk1']).team_id.members.all()
context['team_name'] = team_name
except AttributeError:
pass
return context
那就是我想在那个上下文中添加另一个密钥对,从那里我总是得到一个
Reverse for 'team_select' with keyword arguments '{'pk1': ''}' not found.
1 pattern(s) tried: ['website/project/(?P<pk1>[0-9]+)/linkteam2/$']
我试过了:
def get_context_data(self, **kwargs):
context = super(ProjectDetailView, self).get_context_data(**kwargs)
try:
team_name = Project.objects.get(id=self.kwargs['pk1']).team_id.members.all()
team_score = get_team_cohesivenss_score(self)
m_context = {'team_name':team_name,
'team_score':team_score
}
context = m_context
except AttributeError:
pass
return context
任何想法?
追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/website/project/2/
Django Version: 1.11.5
Python Version: 3.6.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
'website',
'django_extensions',
'crispy_forms',
'survey',
'bootstrapform',
'rosetta',
'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template /Users/raphaelbendenoun/Documents/Django Projects/Authentication_project/website/templates/projectdetailtemp/noteamcreated.html, error at line 8
Reverse for 'team_select' with keyword arguments '{'pk1': ''}' not found. 1 pattern(s) tried: ['website/project/(?P<pk1>[0-9]+)/linkteam2/$'] 1 : {% load static %}
2 : <div class="invite-team">
3 : <div class="jumbotron greenback">
4 : <div class="jumbo-text">
5 : <h7>Your project has been created, It is time to link a team or create a new for your project</h3>
6 : </div>
7 : <div class="jumbo-button">
8 : <a href=" {% url 'website:team_select' pk1=project.id %} " class="btn btn-secondary" role="button"><span class="fa fa-link"></span> Link an existing team</a>
9 : <a href="{% url 'website:add_team' pk1=project.id %}" class="btn btn-success" role="button"><span class="fa fa-plus-circle"></span> Create a new team</a>
10 : </div>
11 : </div>
12 : </div>
13 :
Traceback:
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
217. response = self.process_exception_by_middleware(e, request)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
215. response = response.render()
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/response.py" in render
107. self.content = self.rendered_content
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/response.py" in rendered_content
84. content = template.render(context, self._request)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render
207. return self._render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/loader_tags.py" in render
177. return compiled_parent._render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/loader_tags.py" in render
72. result = block.nodelist.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/defaulttags.py" in render
322. return nodelist.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/loader_tags.py" in render
216. return template.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render
209. return self._render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/defaulttags.py" in render
458. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/urls/base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
497. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /website/project/2/
Exception Value: Reverse for 'team_select' with keyword arguments '{'pk1': ''}' not found. 1 pattern(s) tried: ['website/project/(?P<pk1>[0-9]+)/linkteam2/$']