当我运行URL http://127.0.0.1:8000/crud/add/1
时,它给我错误add() got an unexpected keyword argument 'id'
,有人可以帮助我为什么我收到此错误吗?在这里,我已经添加了我的整个代码,有人可以帮我解决这个问题吗?
urls.py
app_name = 'crud'
urlpatterns = [
path('',views.index, name='index'),
path('add/',views.add, name='add'),
path('add/<int:id>',views.add, name='update'),
]
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from .models import Pages
from django.urls import reverse
from django.shortcuts import get_object_or_404, render
def index(request):
pagelist = Pages.objects.all()
context = {'pagelist': pagelist}
return render(request, 'polls/index.html', context)
def add(request):
if request.POST:
title = request.POST.get("title")
permialink = request.POST.get("permialink")
updated_date = request.POST.get("updated_date")
bodytext = request.POST.get("bodytext")
page_data = Pages(title=title,permialink=permialink,updated_date=updated_date,bodytext=bodytext)
page_data.save()
return HttpResponseRedirect(reverse('crud:index', args=('')))
else:
return render(request,'polls/add.html')
答案 0 :(得分:1)
您正在URL中传递参数,但未在View函数中接收该参数。您应该像这样定义添加功能
def add(request, id=None):
pass
# rest of code
答案 1 :(得分:0)
如果您有方法,例如:
@app.route('/transcript/<int:id>', methods=['GET', 'POST'])
def transcript(employeeId):
if x == 'NoneType':
employee = Employee()
else:
employee = Employee(employeeId)
参数与路由不匹配,例如employeeId
不等于 id
@app.route('/transcript/<int:id>', methods=['GET', 'POST'])
def transcript(id):
if x == 'NoneType':
employee = Employee()
else:
employee = Employee(employeeId)
奇怪的是,我们仍然遇到相同的错误。诀窍是您可能未更改了所有旧的employeeId
参数的旧出现:
@app.route('/transcript/<int:id>', methods=['GET', 'POST'])
def transcript(id):
if x == 'NoneType':
employee = Employee()
else:
employee = Employee(id)