我正在尝试为我所在地区创建一个在线电话簿。我在应用程序的条目创建视图中遇到问题。
这是我的PhoneEntry模型:
# coding=utf-8
from django.urls import reverse_lazy
from django.db import models
from django.template.defaultfilters import slugify
from phonenumber_field.modelfields import PhoneNumberField
class PhoneEntry(models.Model):
# Name of the organisation
org_name = models.CharField(max_length=100, verbose_name="Organisation's Name")
org_details = models.CharField(max_length=500, blank=True, verbose_name="Organisation's Details")
slug = models.SlugField(default='slug', unique=True)
# Verified or not
verified = models.BooleanField(default=False, blank=True)
# Dates when the entry was added and verified
added_date = models.DateField(auto_now_add=True, editable=False)
verified_date = models.DateField(auto_now_add=True)
last_edited_date = models.DateField(blank=True, null=True, auto_now_add=True)
# The phone numbers of the organisation
primary_ph_number = PhoneNumberField(verbose_name="Primary Phone Number")
secondary_ph_number = PhoneNumberField(verbose_name="Secondary Phone Number", blank=True)
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
super(PhoneEntry, self).save(force_insert, force_update, using, update_fields)
self.slug = slugify(self.org_name+"-"+str(int(self.id)))
super(PhoneEntry, self).save(force_insert, force_update, using, update_fields)
@staticmethod
def get_absolute_url():
return reverse_lazy('phbook:index')
def __str__(self):
return self.org_name+"-"+str(self.primary_ph_number)
class Meta:
verbose_name_plural = "Phone Entries"
这就是EntryCreateForm:
class EntryAddForm(forms.ModelForm):
"""org_name = forms.CharField(max_length=100, label="Enter your organisation name: ")
org_details = forms.CharField(max_length=100, widget=forms.Textarea,
label="Enter your organisation details: ", required=False)"""
primary_ph_number = PhoneNumberField(label="Enter your primary phone number: ")
secondary_ph_number = PhoneNumberField(label="Enter your secondary phone number: ", required=False)
class Meta:
model = PhoneEntry
exclude = ['slug', 'last_edited_date', 'added_date', 'verified_date', 'verified']
这就是EntryAddView:
class EntryCreateView(CreateView):
model = PhoneEntry
form_class = EntryAddForm
template_name = 'phbook/form.html'
success_url = 'phbook:index'
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
print("Data is", form.instance)
model = form.save(commit=False)
print(model.org_name, model.org_details, model.primary_ph_number, model.secondary_ph_number)
model.save()
return self.get_success_url()
模板form.html
{% extends 'base.html' %}
{% block head %}
<title>Add your Organisation's Entry | PhoneBook</title>
{% endblock %}
{% block body %}
{% load crispy_forms_tags %}
<form method="post" action="{% url 'phbook:add' %}" enctype="multipart/form-data">
<button type="submit" value="Submit">Submit</button>
{% csrf_token %}
{% crispy form %}
</form>
{% endblock %}
base.html仅包含基础css和js文件的静态链接。
在EntryCreateView中,当执行行print("Data is ", form.instance)
时,它产生了这个结果
请告诉我我在这做什么?
答案 0 :(得分:1)
您已在视图上覆盖了post
,因此绕过了CreateView通常会执行的所有验证调用。很少有任何理由可以覆盖get
或post
方法;你应该总是定义一个更具体的方法;在这种情况下,form_valid
更合适,如果你想要做的就是打印出提交的数据。如果它只是用于调试,那么您可能根本不需要覆盖任何方法。