models.py
class App(models.Model):
name = models.CharField(max_length=10, default='')
desc = models.CharField(max_length=10, default='')
views.py
class AppCreate(CreateView):
template_name = "app/add.html"
model = App
templates
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="" method="POST">
{% csrf_token %}
{{form}}
<input type="submit" value='submit'>
</form>
</body>
</html>
Here two problems:
Why I set model field default value, but it doesn't work in database? I check sql statement, no default value set
If want to set desc as an optional field (desc field in html is not mandatory), what shoud I do?
Default value actually rectifies the need for the optional field. If you do not enter any value, you dont get an error, but the default value is stored, which is of course an empty string(value). To specify optional explicitly, use
blank=Truecan be used