私はウェブページを作成していて、メッセージを残してくれる人を探している連絡先セクションがあり、それから彼らに連絡します。フォームに入力した情報をデータベースに保存して、後で見ることができるようにしたいです。 Django管理ページで。
# models.py
from django.db import models
class Contact(models.Model):
name = models.CharField(max_length = 100, verbose_name = "Nombre")
email = models.CharField(max_length = 100, verbose_name = "Email")
issue = models.CharField(max_length = 200, verbose_name = "Asunto")
text = models.TextField(verbose_name = "Mensaje")
# forms.py
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length = 100, label = "Nombre")
email = forms.EmailField(label = "Correo electrónico")
issue = forms.CharField(max_length = 200, label = "Asunto")
text = forms.CharField(label = "Mensaje")
# views.py
from django.views.generic import TemplateView
from contact.forms import ContactForm
from django.shortcuts import render
class Contact(TemplateView):
template_name = 'contact/contact.html'
def get(self, request):
form = ContactForm
return render(request, self.template_name, {'form': form})
def post(self, request):
form = ContactForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
issue = form.cleaned_data['issue']
text = form.cleaned_data['text']
form = ContactForm()
args = {
'form': form,
'name': name,
'email': email,
'issue': issue,
'text': text,
}
return render(request, self.template_name, args)
<!-- And this is the form -->
<div class="page-section contact-page">
<div class="contact-warp">
<div class="row">
<div class="col-xl-6 p-0">
<div class="contact-text">
<span>¡Hola!</span>
<h2>Contáctame</h2>
<form class="contact-form" method="post">
{% csrf_token %}
{{ form.as_p }}
<button class="site-btn">Enviar mensaje</button>
</form>
</div>
</div>
</div>
</div>
</div>
何度か試みましたが、管理者に表示する情報を取得できません(すでに管理者にモデルを登録しています)。私を助けてください :(
ModelFormを使用していないため、これらの値を手動でビューに保存する必要があります。このような:
def post(self, request):
form = ContactForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
issue = form.cleaned_data['issue']
text = form.cleaned_data['text']
args = {
'form': form,
'name': name,
'email': email,
'issue': issue,
'text': text,
}
Contact.objects.create(**args) # <-- saving to DB
# rest of the code
FormViewはフォームの処理にはるかに適しているため、TemplateViewを使用している理由がわかりません。例えば:
class ContactView(FormView):
form_class = ContactForm
template_name = 'contact/contact.html'
success_url= '/'
def form_valid(self, form):
name = form.cleaned_data['name']
email = form.cleaned_data['email']
issue = form.cleaned_data['issue']
text = form.cleaned_data['text']
args = {
'form': form,
'name': name,
'email': email,
'issue': issue,
'text': text,
}
Contact.objects.create(**args)
return super(ContactView).form_valid(form)
また、ModelFormを使用する場合は、次のようにデータを保存できます。
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = "__all__"
# usage
if form.is_valid():
form.save()
# rest of the code
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加