在我的Django项目中,我想创建一个简单的预订系统,通过单击主页面上的一个按钮来重定向到查看名为book_cd的按钮。在URL中,我们有cd_name(id),在我的视图中转换为var命名名称。问题是我无法使用filter()函数找到我想要更新的一条记录,因为这样的事情会发生:
'WSGIRequest' object has no attribute 'objects'
符合:
results = CD.objects.filter(CD.info.name == name)
我的模特:
class CD(models.Model):
pass
class CDInfo(models.Model):
cd = models.OneToOneField(CD, on_delete=models.CASCADE, primary_key=True, related_name='info')
name = models.CharField(max_length=200)
class CDBooking(models.Model):
cd = models.OneToOneField(CD, on_delete=models.CASCADE, primary_key=True, related_name='booking')
user = models.CharField(max_length=50)
when_booked = models.DateTimeField(timezone.now())
我的观点:
def book_cd(request, CD, cd_name):
name = cd_name
results = CD.objects.filter(CD.info.name == name)
for CD in results:
CD.booking.user = "test"
CD.save()
return redirect('index')
我在函数参数中尝试使用和不使用请求和CD,不知道该怎么做。
提前致谢!
编辑:
完整的views.py:
from django.http import HttpResponse
from .models import CD
from django.template import loader
from django.shortcuts import render, redirect
from HORSeWebSite.forms import SignUpForm
def index(request):
template = loader.get_template("HORSeWebSite/index.html")
list_of_cds = CD.objects.all()
context = {
'CDs': list_of_cds
}
return HttpResponse(template.render(context, request))
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = SignUpForm()
return render(request, 'registration/signup.html', {'form': form})
def book_cd(CD, cd_name):
name = cd_name
results = CD.objects.filter(CD.info.name == name)
for CD in results:
CD.booking.user = "a"
CD.save()
return redirect('index')
urls.py:
from django.urls import path
from . import views
app_name = "HORSeWebSite"
urlpatterns = [
path('', views.index, name='index'),
path('book/<int:cd_name>', views.book_cd, name='cd_name'),
]
回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/HORSeWebSite/book/013
Django Version: 2.0.5
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'HORSeWebSite.apps.HorsewebsiteConfig']
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']
Traceback:
File "C:\Users\eraepkj\PycharmProjects\untitled\venv\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\eraepkj\PycharmProjects\untitled\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\eraepkj\PycharmProjects\untitled\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /HORSeWebSite/book/013
Exception Value: book_cd() missing 1 required positional argument: 'CD'
答案 0 :(得分:0)
更改您的观看代码,如下所示
def book_cd(request, cd_name):
name = cd_name
results = CD.objects.filter(info__name=cd_name)
for cd in results:
cd.booking.user = "a"
cd.save()
return redirect('index')
对于每个基于函数的视图,第一个参数必须是request
,后跟其他参数。