当我调用get_object_or_404()获取我的DetailView的对象时,出现以下错误消息:
get()缺少2个必需的位置参数:“ self”和“ request”
我认为我在概念上缺少一些东西,无法正常解决此问题。任何将我指向正确方向的帮助将不胜感激。
跟踪:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/wines/profiles/justin/cabernet-sauvignon/
Django Version: 2.1.4
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'bootstrap4',
'accounts',
'groups',
'wines',
'posts']
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',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "/anaconda3/envs/wineEnv/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/anaconda3/envs/wineEnv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/anaconda3/envs/wineEnv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/anaconda3/envs/wineEnv/lib/python3.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/anaconda3/envs/wineEnv/lib/python3.7/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/anaconda3/envs/wineEnv/lib/python3.7/site-packages/django/views/generic/detail.py" in get
106. self.object = self.get_object()
File "/Users/ed/code/wine/wineProject/wineProject/wines/views.py" in get_object
40. wp = get_object_or_404(WineProfile, slug=self.kwargs['w_slug'], winemaker=self.winemaker.winemaker_id)
File "/anaconda3/envs/wineEnv/lib/python3.7/site-packages/django/shortcuts.py" in get_object_or_404
93. return queryset.get(*args, **kwargs)
Exception Type: TypeError at /wines/profiles/justin/cabernet-sauvignon/
Exception Value: get() missing 2 required positional arguments: 'self' and 'request'
models.py
class WineMaker(models.Model):
winemaker_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255, unique=True)
slug = AutoSlugField(null=False, unique=True, populate_from='name', default='winemaker')
region = models.ForeignKey(
WineRegion,
db_column = 'region_id',
related_name = 'winemakers',
on_delete = models.CASCADE,
null = True
)
def __str__(self):
return self.name
class Meta:
db_table = 'winemakers'
class WineProfile(models.Model):
id = models.AutoField(primary_key=True)
winemaker = models.ForeignKey(
WineMaker,
db_column = 'winemaker_id',
related_name = 'profiles',
on_delete = models.CASCADE
)
name = models.CharField(max_length=510)
slug = AutoSlugField(null=False, populate_from='name')
description = models.TextField(max_length=10000, null=True)
image = models.ImageField(upload_to='static/wineProject/images/wineProfiles', null=True)
class Meta:
db_table = 'wine_profiles'
unique_together = ('winemaker','slug')
def __str__(self):
return (winemaker.name + ',' + name)
views.py
class WineProfile(DetailView):
model = WineProfile
template_name = 'wines/wine_profile.html'
def get_object(self):
self.winemaker = get_object_or_404(WineMaker, slug=self.kwargs['wm_slug'])
return get_object_or_404(WineProfile, slug=self.kwargs['w_slug'], winemaker=self.winemaker.winemaker_id)
wine_profile.html
{% extends 'wines/wine_base.html' %}
{% block wine_content %}
<p>Wine Profile Page</p>
<p>
Wine Name: {{object.name}} <br>
Wine Description: {{item.description}} <br>
Image: <br>
<h4>Vintages</h4>
{% for wine in wines.profile.id.all %}
{{wine.vintage}} {{wine.name}}
{% endfor %}
</p>
{% endblock %}
urls.py
from django.urls import path, re_path
from wines import views
app_name = 'wines'
urlpatterns = [
path('winemakers/', views.WinemakerList.as_view(), name='winemaker_list'),
path('profiles/<wm_slug>/', views.WineList.as_view(), name='wine_list'),
path('profiles/<wm_slug>/<slug:w_slug>/', views.WineProfile.as_view(), name='wine_profile'),
]
答案 0 :(得分:5)
您有两件叫WineProfile
的东西-您的模型和视图。重命名其中之一-可能应该将视图重命名为WineProfileDetail
。