Im trying to create a model form on django but it doesnt want to render even though I mapped it properly and created the path.
models.py
JavaScript
x
23
23
1
from django.db import models
2
3
# Create your models here.
4
5
Media_Choices = (
6
("TV", "TV"),
7
("Radio", "Radio"),
8
("Youtube", "Youtube"),
9
("Podcast", "Podcast"),
10
)
11
12
class Appear(models.Model):
13
Show = models.CharField(max_length=100)
14
Media = models.CharField(max_length=30, blank=True, null=True, choices=Media_Choices)
15
Episode = models.IntegerField()
16
Date = models.DateField(max_length=100)
17
Time = models.TimeField(auto_now=False, auto_now_add=False)
18
Producer = models.CharField(max_length=100)
19
Producer_Email = models.EmailField(max_length=254)
20
21
def __unicode__(self):
22
return self.Show + ' ' + self.Producer_Email
23
forms.py
JavaScript
1
20
20
1
from django import forms
2
from django.core.exceptions import ValidationError
3
from django.forms import ModelForm
4
from .models import Appear
5
6
class AppsForm(ModelForm):
7
class Meta:
8
model = Appear
9
fields = '__all__'
10
11
def clean_Producer_Email(self):
12
Producer_Email = self.cleaned_data.get('Producer_Email')
13
if (Producer_Email == ""):
14
raise forms.ValidationError('field cannot be left empty')
15
16
for instance in Appear.objects.all():
17
if instance.Producer_Email == Producer_Email:
18
raise forms.ValidationError('Please fill in correct email')
19
return Producer_Emailenter
20
views.py
JavaScript
1
10
10
1
from django.shortcuts import render
2
from .forms import AppsForm
3
4
# Create your views here.
5
6
def AppS(request):
7
form = AppsForm()
8
context = {'forms': form}
9
return render(request, 'AppsForm.html', context)
10
it refuse to render but it displays the html tag that is in the file but not the fields from the form. this is the html template
AppsForm.html
JavaScript
1
16
16
1
{% extends 'base.html' %}
2
3
{% block content %}
4
5
{% load crispy_forms_tags %}
6
7
<form action="" method="POST">
8
9
{% csrf_token %}
10
11
{{ form|crispy }}
12
13
<input type="submit" value="submit">
14
</form>
15
{% endblock %}
16
Advertisement
Answer
you view is wrong try this
JavaScript
1
10
10
1
def AppS(request):
2
if request.method == 'POST':
3
form = AppsForm(request.POST)
4
if form.is_valid():
5
form.save()
6
return redirect('/')
7
else:
8
form = AppsForm()
9
return render(request, 'AppsForm.html', {'form': form})
10
and in your html
JavaScript
1
6
1
<form method="POST" class="" action="">
2
{% csrf_token %}
3
{{ form|crispy }}
4
<input type="submit" class="" value="Submit">
5
</form>
6
now you are good to go and tell me if you still get error