私のモデルに基づいてdjangoでフォームを構築しています。私は私のviews.pyでこれを持っている:フォームの作成のためにviews.pyでクラスを使用するときのdjangoでのフォームアクションの動作
class GroupCreateView(CreateView):
model = Group
form_class = GroupForm
template_name = 'ipaswdb/group/group_form.html'
テンプレートはいくつかの素晴らしいフォームのCSSと日付ピッカーなどで格好良いです。このすべては、私には理にかなって、私は私のforms.py
class GroupForm(forms.ModelForm):
notes=forms.CharField(widget = forms.Textarea)
billing_address = forms.ModelChoiceField(queryset=Address.objects.all(), widget=forms.Select(attrs={'tabindex':'5'}))
start_date = forms.DateField(widget=forms.TextInput(attrs=
{
'class':'datepicker',
'tabindex' : '5',
'placeholder' : 'Groups start date'
}))
class Meta:
model=Group
exclude = ['created_at', 'updated_at']
にウィジェットを追加することができますので、私は、フォームを作成し、私は、フォームが私のモデルをオフに基づいて構築され、物事が移入されますどのように見ることができますModelChoiceFieldで言います私はviews.pyのdef some_methodがどうやって出てくるか分かりません。フォームアクションのフォームテンプレートでは、私はこれを持っています:
<h1> Add a new Group </h1>
<form action="." method="post">
{% csrf_token %}
<div class="col-2">
{{ form.group_name.errors }}
<label>
Group Name:
<input placeholder="Enter the groups name" id="id_group_name" name="group_name" tabindex="1">
</label>
</div>
<div class="col-2">
{{ form.group_contact.errors }}
<label>
Gorup Contact
<input placeholder="Enter the groups contact name" id="id_group_contact" name="group_contact" tabindex="2">
</label>
</div>
<div class="col-2">
{{ form.tin.errors }}
<label>
TIN Number
<input placeholder="Groups TIN#" id="id_tin" name="tin" tabindex="3">
</label>
</div>
<div class="col-2">
{{ form.npi.errors }}
<label>
NPI Number
<input placeholder="Groups NPI#" id="id_npi" name="npi" tabindex="4">
</label>
etc etc etc
私はビューでいくつかのデフォルトメソッドを呼び出していると思いますか?私はその方法が何であるか分かりません。これはまた、新しいグループを追加するためのものです。既存のグループを編集しているケースを処理するために別のビューまたは何かが必要になると思いますか?私が使用していたこのブログのデモは、views.pyでform.pyを使用していましたが、ここではクラスGroupCreateView(CreateView)はありませんでした。例のesqueメソッドviews.pyで作業していましたが、方法(私の方法ではありません注意してください):
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
comments = post.comments.filter(active=True)
#their form stuff
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.post = post
new_comment.save()
else:
comment_form=CommentForm()
return render(request,
'blog/post/detail.html',
{'post': post, 'comments':comments, 'comment_form':comment_form})
私の質問がある(と私はそれを引用するのかの例を覚えてすることはできません)が、class GroupCreateView(CreateView):
が本当に何をやっているとどのように私はそれが/参照フォームが戻ってきて作成し得ることができます最終的に私がデータベースに検証して保存できる正しい行動を呼び出すことができますか?
また、新しいグループを追加している場合や、既存のグループを編集している別のケースを処理するために、これを(大まかに)拡張することができますか? (私はこの最初の答えに関係していると確信しているので、ここでこの2番目の質問をします)。 P、ジェネリッククラスには二つの方法があります:
url(r'group/add/$', GroupCreateView.as_view(), name='group-add'),
フォームアクションはDjangoとは関係ありません。フォームは、フォームが送信するURLです。 '"は現在のパスを意味するショートカットです。 –
これは、どのメソッドがこれを処理するかを指定するために、urls.pyのステートメントが必要なことを意味します。 – Codejoy
もちろん、もちろんですが、要点はフォームを表示して処理するURLと同じです。 –