我一直坚持这个问题 - 表单能够提交但不会保存到数据库中。我甚至通过一个表单发送了一封电子邮件,但它不会保存。我正在制作CRM并拥有联系人,通信和电子邮件应用程序。到目前为止,该视图有三种形式,每种模型一种。联系表单可以保存到DB。其他两个能够提交但不会保存。这两种不保存的表单都可以重定向回刷新的联系人详细信息,其实例和数据仍可在表单上获得。所以它必须正确存储,而不是保存。我不确定我错过了什么。如果您需要我添加其他文件,请告诉我。
views.py ----------
def contact_detail(request, contact_id):
contact = get_object_or_404(Contact, pk=contact_id)
if request.method == 'POST' and 'contact' in request.POST:
contactForm = ContactForm(request.POST, prefix='contact', instance=contact)
if contactForm.is_valid():
contactForm.save()
else:
contactForm = ContactForm(prefix='contact', instance=contact)
if request.method == 'POST' and 'email' in request.POST:
contactForm = ContactForm(prefix='contact', instance=contact)
messageForm = MessageForm(request.POST, prefix='email', instance=contact)
if messageForm.is_valid():
messageForm.save()
form_email = contact.contact_email
form_message = messageForm.cleaned_data.get("message")
subject = messageForm.cleaned_data.get("subject")
from_email = settings.EMAIL_HOST_USER
to_email = [from_email]
contact_message = "%s" % (
form_message)
send_mail(subject,
contact_message,
from_email,
to_email,
fail_silently=True)
else:
messageForm = MessageForm(prefix='email', instance=contact)
company = Company.objects.filter(contact=contact_id)
communications = Communication.objects.filter(contact=contact_id).order_by('-comm_date')
if request.method == 'POST' and 'comm' in request.POST:
contactForm = ContactForm(prefix='contact', instance=contact)
messageForm = MessageForm(prefix='email', instance=contact)
commForm = CommForm(request.POST, prefix='comm', instance=contact)
if commForm.is_valid():
commForm.save()
redirect_url = reverse(
'contacts:contact_detail',
args=(contact_id,)
)
return HttpResponseRedirect(redirect_url)
else:
commForm = CommForm(prefix='comm')
context = {
'contact': contact,
'contactForm': contactForm,
'company': company,
'messageForm': messageForm,
'communications': communications,
'commForm': commForm}
return render(request, 'contacts/contact_detail.html', context)
forms.py ----------
from django import forms
from communications.models import Communication
from django.forms import ModelForm
class CommForm(ModelForm):
class Meta:
model = Communication
fields = '__all__'
comm_form.html ----------
<form action='' method='post'>{% csrf_token %}
{% for field in commForm %}
<div class='fieldWrapper'>
{% csrf_token %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<p><input type='submit' name='comm' value='submit comm' /></p>
</form>
models.py ----------
from django.contrib.auth.models import User
from companies.models import Company
from contacts.models import Contact
class Communication(models.Model):
company = models.ForeignKey(Company, null=True, blank=True)
contact = models.ForeignKey(Contact, null=True, blank=True)
owner = models.ForeignKey(User, blank=True)
comm = models.TextField()
comm_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
答案 0 :(得分:0)
找出答案,并在此处发布,以防有人来这里提出类似的问题。
我的表单没有保存到数据库的原因是因为我正在使用表单的实例并寻求添加到db:
commForm = CommForm(request.POST, prefix='comm', instance=contact)
如果要将添加添加到数据库,则无法使用实例。实例只能用于更新数据库。
如果您在获取要保存到数据库的表单时遇到问题,可能是因为您尚未调用实例。