[英]Card() got an unexpected keyword argument 'initial'
I'm putting together a CRUD interface without using the admin app, still pretty new to Python and Django, but when I go to create an entry, it throws out the error in my title.我在不使用管理应用程序的情况下组合了一个 CRUD 界面,对于 Python 和 Django 来说仍然很新,但是当我在 go 中创建一个条目时,它会抛出错误。
here's the target page:这是目标页面:
<form method="post">
{% csrf_token %}
Card Name:<br>
<input type="text" name="name"><br>
Mana Cost:<br>
<input type="text" name="mana_cost"><br>
Supertype:<br>
<input type="text" name="supertype"><br>
Keyword 1:<br>
<input type="text" name="keyword_1"><br>
Keyword 2:<br>
<input type="text" name="keyword_2"><br>
Keyword 3:<br>
<input type="text" name="keyword_3"><br>
Rules Text:<br>
<input type="text" name="rules_text"><br>
Power & Toughness:<br>
<input type="text" name="power_toughness"><br>
<input type="submit" value="Add Card"><br>
{% endblock %}
my views.py:我的意见.py:
from task99_app import forms
from django.views.generic import ListView
from django.views.generic import DetailView
from django.views.generic.edit import CreateView
from django.views.generic.edit import UpdateView
from django.views.generic.edit import DeleteView
from .models import Card
# Create your views here.
def home(request):
return render(request, 'home.html')
class CardList(ListView):
model = Card
class CardCreate(CreateView):
form_class = Card
class CardDetail(DetailView):
model = Card
class CardUpdate(UpdateView):
model = Card
class CardDelete(DeleteView):
model = Card
my forms.py:我的 forms.py:
from django import forms
class CardForm(forms.ModelForm):
name = forms.CharField(max_length=80)
mana_cost = forms.CharField(max_length=12)
supertype = forms.CharField(max_length=30)
keyword_1 = forms.CharField(max_length=20)
keyword_2 = forms.CharField(max_length=20)
keyword_3 = forms.CharField(max_length=20)
rules_text = forms.CharField(max_length=500)
power_toughness = forms.CharField(max_length=10)
and my models.py:和我的models.py:
from django import forms
class CardForm(forms.ModelForm):
name = forms.CharField(max_length=80)
mana_cost = forms.CharField(max_length=12)
supertype = forms.CharField(max_length=30)
keyword_1 = forms.CharField(max_length=20)
keyword_2 = forms.CharField(max_length=20)
keyword_3 = forms.CharField(max_length=20)
rules_text = forms.CharField(max_length=500)
power_toughness = forms.CharField(max_length=10)
I've seen a few instances of this happening to others, but I don't understand their code well enough to understand how to fix mine, but I feel so close.我已经看到其他人发生这种情况的一些例子,但我对他们的代码理解得不够好,无法理解如何修复我的代码,但我感觉如此接近。
EDIT: urls.py:编辑:urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from task99_app import views
urlpatterns = [
url('admin/', admin.site.urls),
url(r'form/$', views.CardCreate.as_view(), name='card_create'),
url(r'^$', views.home, name='home'),
url(r'card_list/$', views.CardList.as_view(), name='card_list'),
url(r'card/<int:pk>', views.CardDetail.as_view(), name='card_details'),
url(r'delete/<int:pk>', views.CardDelete.as_view(), name='card_delete'),
url(r'update/<int:pk>', views.CardUpdate.as_view(), name='card_update'),
]
In your CardCreate
view, you have used the model as form_class
, not your Form
:在您的
CardCreate
视图中,您使用了model作为form_class
,而不是您的Form
:
class CardCreate(CreateView):
model = Card
form_class =
CardForm
EDIT 1 : Note that in your UpdateView
, you forgot to set a form_class
.编辑 1 :请注意,在您的
UpdateView
中,您忘记设置form_class
。 That is not per se necessary, but then you need to specify fields
[Django-doc] .这本身不是必需的,但是您需要指定
fields
[Django-doc] 。 You can however use the same form_class
:但是,您可以使用相同的
form_class
:
class CardUpdate(UpdateView):
model = Card
form_class =
CardForm
Furthermore you need to specify the model in your CardForm
:此外,您需要在 CardForm 中指定
CardForm
:
class CardForm(forms.ModelForm):
name = forms.CharField(max_length=80)
mana_cost = forms.CharField(max_length=12)
supertype = forms.CharField(max_length=30)
keyword_1 = forms.CharField(max_length=20)
keyword_2 = forms.CharField(max_length=20)
keyword_3 = forms.CharField(max_length=20)
rules_text = forms.CharField(max_length=500)
power_toughness = forms.CharField(max_length=10)
class Meta:
model = Card
fields = '__all__'
EDIT 2 : Note that in your urls.py
you use path syntax in url(..)
s, you should use path(..)
[Django-doc] instead:编辑 2 :请注意,在您的
urls.py
中,您在url(..)
中使用路径语法,您应该使用path(..)
[Django-doc]代替:
urlpatterns = [
path('admin/', admin.site.urls
),
path('form/', views.CardCreate.as_view(), name='card_create'
),
path('', views.home, name='home'
),
path('card_list/', views.CardList.as_view(), name='card_list'
),
path('card/<int:pk>', views.CardDetail.as_view(), name='card_details'
),
path('delete/<int:pk>', views.CardDelete.as_view(), name='card_delete'
),
path('update/<int:pk>', views.CardUpdate.as_view(), name='card_update'
),
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.