After creating the initial data (CreateView) I then proceed to update more data (UpdateView), yet I get the following answer while tring to update the new data. I am attaching the relvelant parts of the code:
ValueError at /preinscripcion/padres/63
Cannot assign None: "Alumni.cycle" does not allow null values.
Request Method: POST
Request URL: http://decroly.administracionescolarmexico.com/preinscripcion/padres/63
Django Version: 1.5
Exception Type: ValueError
Exception Value:
Cannot assign None: "Alumni.cycle" does not allow null values.
Exception Location: /home/fbenavides/webapps/django/lib/python2.7/django/db/models/fields/related.py in __set__, line 401
Python Executable: /usr/local/bin/python
Python Version: 2.7.5
Model.py
# -*- coding: utf-8 -*-
# Create your models here
from django.db import models
from django.forms import ModelForm
from django.conf import settings
from django.core.files.storage import FileSystemStorage
import os.path
import datetime
# Create your models here
class ScholarCycle(models.Model):
"""
ScholarCycle
Yearly School Cycle (e.g. 2012-2013)
"""
cycle = models.CharField(blank=False, max_length=9, verbose_name=u'Ciclo Escolar')
def __unicode__(self):
return u'%s' % self.cycle
class Meta:
db_table = u'Ciclo Escolar'
ordering = ['id', 'cycle']
verbose_name = u'Ciclo Escolar'
verbose_name_plural = u'Ciclos Escolares'
class Admin:
pass
fs_photos = FileSystemStorage(location='/media/user/photos')
fs_docs = FileSystemStorage(location='/media/user/docs')
class Alumni(models.Model):
"""
"""
cycle = models.ForeignKey(ScholarCycle, unique=False, null=False, blank=True, verbose_name=u'Ciclo Escolar')
url.py
urlpatterns = patterns('',
url(r'^lista/$', views.AlumniList.as_view(), name='alumni-lista'),
url(r'^lista/(?P<page>\d+)$', views.AlumniRedirectMostrar.as_view(), name='alumni-redirect-mostrar'),
url(r'^mostrar/(?P<pk>\d+)$', views.AlumniRetrieve.as_view(), name='alumni-mostrar'),
url(r'^mostrar/padres/(?P<pk>\d+)$', views.AlumniRetrievePadres.as_view(), name='alumni-mostrar-padres'),
url(r'^mostrar/autorizado/(?P<pk>\d+)$', views.AlumniRetrieveAutorizado.as_view(), name='alumni-mostrar-autorizado'),
url(r'^mostrar/finanzas/(?P<pk>\d+)$', views.AlumniRetrieveFinanzas.as_view(), name='alumni-mostrar-finanzas'),
url(r'^mostrar/documentos/(?P<pk>\d+)$', views.AlumniRetrieveDocumentos.as_view(), name='alumni-mostrar-documentos'),
url(r'^inscripcion/$', views.AlumniRegister.as_view(), name='alumni-inscripcion'),
url(r'^inscripcion/padres/(?P<pk>\d+)$', views.AlumniRegisterPadres.as_view(), name='alumni-inscripcion-padres'),
url(r'^inscripcion/autorizado/(?P<pk>\d+)$', views.AlumniRegisterAutorizado.as_view(), name='alumni-inscripcion-autorizado'),
url(r'^inscripcion/finanzas/(?P<pk>\d+)$', views.AlumniRegisterFinanzas.as_view(), name='alumni-inscripcion-finanzas'),
url(r'^inscripcion/documentos/(?P<pk>\d+)$', views.AlumniRegisterDocumentos.as_view(), name='alumni-inscripcion-documentos'),
url(r'^preinscripcion/$', views.AlumniPreinscripcion.as_view(), name='alumni-preinscripcion'),
url(r'^preinscripcion/padres/(?P<pk>\d+)$', views.AlumniPreinscripcionPadres.as_view(), name='alumni-preinscripcion-padres'),
url(r'^preinscripcion/autorizados/(?P<pk>\d+)$', views.AlumniPreinscripcionAutorizados.as_view(), name='alumni-preinscripcion-autorizados'),
url(r'^preinscripcion/finanzas/(?P<pk>\d+)$', views.AlumniPreinscripcionFinanzas.as_view(), name='alumni-preinscripcion-finanzas'),
views.py
class AlumniPreinscripcion(LoginRequiredMixin, CreateView):
model = models.Alumni
form_class = forms.AlumniFormPreinscripcion
context_object_name = 'alumno'
template_name = 'alumni_preinscripcion.html'
def get_initial(self):
initial = super(AlumniPreinscripcion, self).get_initial()
initial['cycle'] = models.ScholarCycle.objects.get(cycle__exact='2014-2015')
return initial
def get_success_url(self, **kwargs):
return reverse('alumni-preinscripcion-padres', kwargs={'pk':self.object.id})
class AlumniPreinscripcionPadres(LoginRequiredMixin, UpdateView):
model = models.Alumni
form_clas = forms.AlumniFormPreinscripcionPadres
context_object_name = 'alumno'
template_name = 'alumni_preinscripcion_padres.html'
def get_sucess_url(self, **kwargs):
return reverse('alumni-preinscripcion-autorizados', kwargs='pk':self.object.id})
alumni_preinscripcion_padres.html
{% extends "preinscripcion.html" %}
{% load i18n %}
{% load l10n %}
{% block content %}
<h2>Padres</h2>
<form action="" method="post" enctype="multipart/form-data" accept-charset="utf-8">
{% csrf_token %}
<fieldset>
<legend><strong> Papá </strong></legend>
<div class="row-fluid">
<div class="span2">
<p>Nombre:</p>
</div>
<div class="span10">
<p>{{ form.father_name }} {{ form.father_familynames }}</p>
</div>
</div>
<br />
<p><input type="submit" value="Persona(s) Autorizada(s) →" /></p>
</form>
{% endblock %}
The cycle field on your Alumni model is defined with null=False, blank=True. You probably want to change that. I can't tell you the correct values because that depends on your application logic, but those values are working at odds with your form views. From the documention:
null is purely database-related, whereas blank is validation-related.
If a field has blank=True, form validation will allow entry of an
empty value. If a field has blank=False, the field will be required.
Read that entire section. Once you understand the proper settings for your Model fields, the forms and class-based-views will most likely work just fine. (FYI, the reason your CreateView works is because you specifically set initial['cycle'] which satisfies the null criteria whereas you don't do that in UpdateView and the update form thinks it's okay to have blank/empty values.)
Update: There are two typos in your AlumniPreinscripcionPadres view, get_sucess_url() and form_clas, both missing a letter.
Related
Is there a way to do this?
Below is an example of what I'm trying to do:
class TestForm(FlaskForm):
email = EmailField('Email', validators=[InputRequired('Email is required.')])
start = SubmitField()
Then in a route:
del form.email.validators
# I also tried:
form.email.validators.remove
Basically I want to use stored data to determine if the field should be required or not for a predefined form.
Dynamically create an internal subclasses of the form within your view. Remove any validators from the fields of the internal subclass and then instance a form from the internal subclass. In code, something like:
Define a form, the first_name field has two validators.
class TestForm(FlaskForm):
first_name = StringField(validators=[InputRequired(), Length(8)])
submit = SubmitField()
In your view:
# Dynamic subclass
class F(TestForm):
pass
# Remove the length validator.
# Validators are in a dict called kwargs
validators = F.first_name.kwargs.get('validators')
for validator in validators:
if isinstance(validator, Length):
validators.remove(validator)
# instance a form
_form = F()
# use the form ....
Single file example app.py:
from flask import Flask, render_template_string
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import InputRequired, Length
app = Flask(__name__)
app.config['SECRET_KEY'] = '13332311ecd738748f27a992b6189d3f5f30852345a1d5261e3e9d5a96722fb9'
class TestForm(FlaskForm):
first_name = StringField(validators=[InputRequired(), Length(8)])
submit = SubmitField()
html_template = '''
{% if form.first_name.errors %}
<ul class="errors">
{% for error in form.first_name.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<form role="form" method="post" action="" name="Form1">
{{ form.hidden_tag() }}
{{ form.first_name }}
{{ form.submit() }}
</form>
'''
success_template = '''
<h1>Success</h1>
'''
#app.route('/', methods=['GET', 'POST'])
def index():
# Dynamic subclass
class F(TestForm):
pass
# Remove the length validator.
# Validators are in a dict called kwargs
validators = F.first_name.kwargs.get('validators')
for validator in validators:
if isinstance(validator, Length):
validators.remove(validator)
# instance a form
_form = F()
# print the validators
for field in _form:
print(f"Field: {field.name} has {len(field.validators)} validator(s)")
for validator in field.validators:
print(validator)
if _form.validate_on_submit():
print('Validation passed')
return render_template_string(success_template)
return render_template_string(html_template, form=_form)
if __name__ == '__main__':
app.run()
You can try:
form.email.validators = ()
This worked well for me.
You can also add back the validators with:
form.email.validators = [InputRequired('Email is required')]
I have seen this approach in many web applications (e.g. when you subscribe for an insurance), but I can't find a good way to implement it in django. I have several classes in my model which inherit from a base class, and so they have several fields in common. In the create-view I want to use that inheritance, so first ask for the common fields and then ask for the specific fields, depending on the choices of the user.
Naive example, suppose I want to fill a database of places
class Place(Model):
name = models.CharField(max_length=40)
address = models.CharField(max_length=100)
class Restaurant(Place):
cuisine = models.CharField(max_length=40)
website = models.CharField(max_length=40)
class SportField(Place):
sport = models.CharField(max_length=40)
Now I would like to have a create view when there are the common fields (name and address) and then the possibility to choose the type of place (Restaurant / SportField). Once the kind of place is selected (or the user press a "Continue" button) new fields appear (I guess to make it simple the page need to reload) and the old one are still visible, already filled.
I have seen this approach many times, so I am surprised there is no standard way, or some extensions already helping with that (I have looked at Form Wizard from django-formtools, but not really linked to inheritance), also doing more complicated stuff, as having more depth in inheritance.
models.py
class Place(models.Model):
name = models.CharField(max_length=40)
address = models.CharField(max_length=100)
class Restaurant(Place):
cuisine = models.CharField(max_length=40)
website = models.CharField(max_length=40)
class SportField(Place):
sport = models.CharField(max_length=40)
forms.py
from django.db import models
from django import forms
class CustomForm(forms.Form):
CHOICES = (('restaurant', 'Restaurant'), ('sport', 'Sport'),)
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Name'}))
address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Address'}))
type = forms.ChoiceField(
choices=CHOICES,
widget=forms.Select(attrs={'onChange':'renderForm();'}))
cuisine = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Cuisine'}))
website = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Website'}))
sport = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Sport'}))
views.py
from django.http.response import HttpResponse
from .models import Restaurant, SportField
from .forms import CustomForm
from django.shortcuts import render
from django.views import View
class CustomView(View):
def get(self, request,):
form = CustomForm()
return render(request, 'home.html', {'form':form})
def post(self, request,):
data = request.POST
name = data['name']
address = data['address']
type = data['type']
if(type == 'restaurant'):
website = data['website']
cuisine = data['cuisine']
Restaurant.objects.create(
name=name, address=address, website=website, cuisine=cuisine
)
else:
sport = data['sport']
SportField.objects.create(name=name, address=address, sport=sport)
return HttpResponse("Success")
templates/home.html
<html>
<head>
<script type="text/javascript">
function renderForm() {
var type =
document.getElementById("{{form.type.auto_id}}").value;
if (type == 'restaurant') {
document.getElementById("{{form.website.auto_id}}").style.display = 'block';
document.getElementById("{{form.cuisine.auto_id}}").style.display = 'block';
document.getElementById("{{form.sport.auto_id}}").style.display = 'none';
} else {
document.getElementById("{{form.website.auto_id}}").style.display = 'none';
document.getElementById("{{form.cuisine.auto_id}}").style.display = 'none';
document.getElementById("{{form.sport.auto_id}}").style.display = 'block';
}
}
</script>
</head>
<body onload="renderForm()">
<form method="post" action="/">
{% csrf_token %}
{{form.name}}<br>
{{form.address}}<br>
{{form.type}}<br>
{{form.website}}
{{form.cuisine}}
{{form.sport}}
<input type="submit">
</form>
</body>
</html>
Add templates folder in settings.py
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
]
I've created a 2-page working example using modified Class Based Views.
When the form is submitted on the first page, an object of place_type is created. The user is then redirected to the second page where they can update existing details and add additional information.
No separate ModelForms are needed because the CreateView and UpdateView automatically generate the forms from the relevant object's model class.
A single template named place_form.html is required. It should render the {{ form }} tag.
# models.py
from django.db import models
from django.urls import reverse
class Place(models.Model):
"""
Each tuple in TYPE_CHOICES contains a child class name
as the first element.
"""
TYPE_CHOICES = (
('Restaurant', 'Restaurant'),
('SportField', 'Sport Field'),
)
name = models.CharField(max_length=40)
address = models.CharField(max_length=100)
place_type = models.CharField(max_length=40, blank=True, choices=TYPE_CHOICES)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('place_update', args=[self.pk])
# Child models go here...
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('create/', views.PlaceCreateView.as_view(), name='place_create'),
path('<pk>/', views.PlaceUpdateView.as_view(), name='place_update'),
]
# views.py
from django.http import HttpResponseRedirect
from django.forms.models import construct_instance, modelform_factory
from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse_lazy
from . import models
class PlaceCreateView(CreateView):
model = models.Place
fields = '__all__'
def form_valid(self, form):
"""
If a `place_type` is selected, it is used to create an
instance of that Model and return the url.
"""
place_type = form.cleaned_data['place_type']
if place_type:
klass = getattr(models, place_type)
instance = klass()
obj = construct_instance(form, instance)
obj.save()
return HttpResponseRedirect(obj.get_absolute_url())
return super().form_valid(form)
class PlaceUpdateView(UpdateView):
fields = '__all__'
success_url = reverse_lazy('place_create')
template_name = 'place_form.html'
def get_object(self, queryset=None):
"""
If the place has a `place_type`, get that object instead.
"""
pk = self.kwargs.get(self.pk_url_kwarg)
if pk is not None:
obj = models.Place.objects.get(pk=pk)
if obj.place_type:
klass = getattr(models, obj.place_type)
obj = klass.objects.get(pk=pk)
else:
raise AttributeError(
"PlaceUpdateView must be called with an object pk in the URLconf."
)
return obj
def get_form_class(self):
"""
Remove the `place_type` field.
"""
model = self.object.__class__
return modelform_factory(model, exclude=['place_type',])
We did something similar manually, we created the views and forms based on design and did the linkage based on if conditions.
I think a nice solution would be to dynamically access subclasses of the main class and then do the necessary filtering/lists building.
UPD: I've spent some more time today on this question and made a "less raw" solution that allows to use the inheritance.
You can also check the code below deployed here. It has only one level of inheritance (as in example), though, the approach is generic enough to have multiple levels
views.py
def inheritance_view(request):
all_forms = {form.Meta.model: form for form in forms.PlaceForm.__subclasses__()}
all_forms[models.Place] = forms.PlaceForm
places = {cls._meta.verbose_name: cls for cls in models.Place.__subclasses__()}
# initiate forms with the first one
context = {
'forms': [forms.PlaceForm(request.POST)],
}
# check sub-forms selected on the forms and include their sub-forms (if any)
for f in context['forms']:
f.sub_selected = request.POST.get('{}_sub_selected'.format(f.Meta.model._meta.model_name))
if f.sub_selected:
sub_form = all_forms.get(places.get(f.sub_selected))
if sub_form not in context['forms']:
context['forms'].append(sub_form(request.POST))
# update some fields on forms to render them on the template
for f in context['forms']:
f.model_name = f.Meta.model._meta.model_name
f.sub_forms = {x.Meta.model._meta.verbose_name: x for x in f.__class__.__subclasses__()}
f.sub_options = f.sub_forms.keys() # this is for rendering selector on the form for the follow-up forms
page = loader.get_template(template)
response = HttpResponse(page.render(context, request))
return response
forms.py
class PlaceForm(forms.ModelForm):
class Meta:
model = models.Place
fields = ('name', 'address',)
class RestaurantForm(PlaceForm):
class Meta:
model = models.Restaurant
fields = ('cuisine', 'website',)
class SportFieldForm(PlaceForm):
class Meta:
model = models.SportField
fields = ('sport',)
templates/inheritance.html
<body>
{% for form in forms %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
{% if form.sub_options %}
<select class="change-place" name="{{ form.model_name }}_sub_selected">
{% for option in form.sub_options %}
<option value="{{ option }}" {% if option == form.sub_selected %}selected{% endif %}>{{ option }}</option>
{% endfor %}
</select>
{% endif %}
<button type="submit">Next</button>
</form>
{% endfor %}
</body>
What I didn't make here is saving the form to the database. But it should be rather trivial using the similar snippet:
for f in context['forms']:
if f.is_valid():
f.save()
Add a PlaceType table, and a FK, e.g. type_of_place, to the Place table:
class PlaceType(Model):
types = models.CharField(max_length=40) # sportsfield, restaurants, bodega, etc.
class Place(Model):
name = models.CharField(max_length=40)
address = models.CharField(max_length=100)
type_of_place = models.ForeignKey('PlaceType', on_delete=models.SET_NULL, null=True)
class Restaurant(Place):
cuisine = models.CharField(max_length=40)
website = models.CharField(max_length=40)
This allows you to create a new Place as either SportsField, restaurant or some other type which you can easily add in the future.
When a new place is created, you'll use the standard CreateView and Model Form. Then, you can display a second form which also uses a standard CreateView that is based on the type_of_place value. These forms can be on the same page (and with javascript on the browser side, you'll hide the second form until the first one is saved) or on separate pages--which may be more practical if you intend to have lots of extra columns. The two key points are as follows:
type_of_place determines which form, view, and model to use. For
example, if user chooses a "Sports Field" for type_of_place, then
you know to route the user off to the SportsField model form;
CreateViews are designed for creating just one object/model. When
used as intended, they are simple and easy to maintain.
There are lot of way you can handle multiple froms in django. The easiest way to use inlineformset_factory.
in your froms.py:
forms .models import your model
class ParentFrom(froms.From):
# add fields from your parent model
Restaurant = inlineformset_factory(your parent model name,Your Child model name,fields=('cuisine',# add fields from your child model),extra=1,can_delete=False,)
SportField = inlineformset_factory(your parent model name,Your Child model name,fields=('sport',# add fields from your child model),extra=1,can_delete=False,)
in your views.py
if ParentFrom.is_valid():
ParentFrom = ParentFrom.save(commit=False)
Restaurant = Restaurant(request.POST, request.FILES,) #if you want to add images or files then use request.FILES.
SportField = SportField(request.POST)
if Restaurant.is_valid() and SportField.is_valid():
ParentFrom.save()
Restaurant.save()
SportField.save()
return HttpResponseRedirect(#your redirect url)
#html
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
#{{ Restaurant.errors}} #if you want to show error
{{ Restaurant}}
{{ SportField}}
{{form}}
</form>
you can use simple JavaScript in your html for hide and show your any froms fields
i am new to django and i am unable to get to print to the html page.How do you display the information retrieved from the database into a blank html page?
blank.hml
<body>
<h1>Classes Added</h1>
{% for i in classInfo %}
<h1>{{ i.label }}</h1>
{% endfor %}
</body>
models.py
class EventType(models.Model):
'''
Simple ``Event`` classifcation.
'''
objects = models.Manager()
abbr = models.CharField(_('abbreviation'), max_length=4, unique=False)
label = models.CharField(_('label'), max_length=50)
class Meta:
verbose_name = _('event type')
verbose_name_plural = _('event types')
def __str__(self):
return self.label
views.py
def displayClass(TemplateView):
templateName = 'blank.html'
def get(self,request):
form = ClassCreationForm()
classInfo = EventType.objects.all()
print(classInfo)
args = {'form' : form, 'classInfo' : classInfo}
return render(request,self,templateName,{'form':form})
forms.py
class ClassCreationForm(forms.Form):
classroom = forms.CharField(label = 'Class Name',max_length=50)
I think you need to understand how the views.py file works. You should have your business logic included in there and the pass it along to the template to be rendered. It can be passed along by the return feature you have included in your code. Although, in your return feature you are only passing a templatename and the form you wanted to render. There isn't data related to the EventType queryset being passed to your template as it is not included in the return context.
Now, personally I like working with Django Class-Based-generic-Views (CBV), since a lot of the code is included in there for you. I am not sure if you have got to the point of learning these yet but I would check them out.
If you would like to add a form into this, you could do so by adding FormMixin which is part of the generic mixins Django provides.
How I would structure your view.py code using generic views is as follows:
from django.views import generic
from django.views.generic.edit import FormMixin
from YourApp.forms import ClassCreationForm
from YourApp.models import EventType
class DisplayClass(FormMixin,generic.ListView):
template_name = 'blank.html'
form_class = ClassCreationForm
def get_queryset(self, *args, **kwargs):
return EventType.objects.all()
If you decide to use class based views you will need to add additional criteria to your urls.py file (the .as_view()):
from django.urls import path
from . import views
urlpatterns = [
path('yoururlpath/', views.DisplayClass.as_view()),
]
Then in your template:
{% for i in object_list %}
<h1>{{ i.label }}</h1>
{% endfor %}
rendering your form...
{{ form.as_p }}
I'm using smart-selects.
Edited : Here is the code that works for me, after adding {{ form.media.js }} in the template , thanks to #Evangelos
models.py :
from django.db import models
from smart_selects.db_fields import ChainedForeignKey, ChainedManyToManyField
class ChoixTangente(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class ChoixModele(models.Model):
name = models.CharField(max_length=255)
tangentes = models.ManyToManyField('Choixtangente', blank=True)
def __str__(self):
return self.name
class TypeModele(models.Model):
tangente = models.ForeignKey(ChoixTangente, blank=True, null=True)
type_modele = ChainedForeignKey(
ChoixModele,
chained_field="tangente",
chained_model_field="tangentes",
show_all=False,
auto_choose=True,
blank=True, null=True
)
def __unicode__(self):
return str(self.pk)
form.py :
from django import forms
from .models import ChoixTangente, ChoixModele, TypeModele
class TypeModeleForm(forms.ModelForm):
class Meta:
model = TypeModele
fields = ('tangente', 'type_modele')
views.py
from .models import ChoixTangente, ChoixModele, TypeModele
from .forms import TypeModeleForm
def type_modele_new(request):
if request.method == "POST":
form = TypeModeleForm(request.POST)
if form.is_valid():
modele_instance = form.save()
return redirect('calculs.views.type_modele_detail', pk=modele_instance.pk)
else:
form = TypeModeleForm()
return render(request, 'calculs/type_modele_new.html', {'form': form})
def type_modele_detail(request, pk):
modele_instance = get_object_or_404(TypeModele, pk=pk)
return render(request, 'calculs/type_modele_detail.html', {'modele_instance': modele_instance})
template : type_modele_new.html
{% load staticfiles %}
<form method="POST">
{% csrf_token %}
**{{ form.media.js }}**
{{ form.as_p}}
<input type="submit" value="Submit">
</form>
urls.py
from django.conf.urls import url, include
from . import views
from smart_selects import urls as smart_selects_urls
urlpatterns = [
url(r'^chaining/', include('smart_selects.urls')),
url(r'^type_modele/new/$', views.type_modele_new, name='modele_new'),
url(r'^type_modele/(?P<pk>[0-9]+)/$', views.type_modele_detail, name='modele_detail'),
]
Edited : and here was the problem :
I have it working fine in admin :
I create ChoixTangente instances
I create ChoixModele
instances and select ChoixTangentes instances in the list
I can create TypeModele" instances by selecting "tangente" in a list of ChoixTangente and "type_modele" in the list of corresponding ChoixModele choices resulting from the steps 1) and 2)
see Admin form screenshot
I whish to have my users do the same through a form. But I can't have it working.
the field "Tangente" is populated with the list of ChoixTangente but when I choose a value the field "type_modele" stays empty instead of displaying a list of corresponding choices.
see form screenshot for the users
First of all where is your views?
Try to post more complete question with files for better understanding.
Where is the model Appareil in the models.py you are refering to in forms.py?
And your fields should be in list [].
Your forms.py should look like:
from django import forms
from .models import Modele
class AppareilForm(forms.ModelForm):
class Meta:
model = Modele
fields =['tangente', 'modele']
You don't need to write javascript.
Just remember to include url(r'^chaining/', include('smart_selects.urls')) to your urls.py
in your view call the AppareilForm Read about forms in views for details.
And in the template just use {{form.as_p}} and {{ form.media.js }} before to load javascript and make sure to load static in your template. {% load static % }
Your template should look something like this
{% load static % }
<form action="your url that points to your view" method="POST">
{% csrf_token %}
{{ form.media.js }}
{{ form.as_p}}
<input type="submit" value="Submit">
</form>
Read the django-selects description carefully.
What I am trying to do is the following: User will have a production (also known as podcast episode) already created with the necessary info until this point (production_id would be the id for this query). The idea is, when user arrives to ChapterMark template, he would be able to create several timestamps to point out certain topics he/she is talking throughout his/her episode. chaptermark_id is created since it would be a One-To-Many and with this id I can add as much timestamps as I want within that episode. With this in mind, which is the best approach for this type of situation and how I can implement it in my form, class view and template?
Thanks in advance
Here is my views.py:
from django.http import HttpResponseRedirect, Http404, HttpResponseForbidden
from django.shortcuts import render, get_object_or_404
from django.views.generic import View, RedirectView, TemplateView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from .forms.client_setup import ClientSetupForm
from .forms.podcast_setup import PodcastSetupForm
from .forms.episode_info import EpisodeInfoForm
from .forms.image_files import EpisodeImageFilesForm
from .forms.wordpress_info import EpisodeWordpressInfoForm
from .forms.chapter_marks import EpisodeChapterMarksForm
from .forms.show_links import ShowLinksForm
from .forms.tweetables import TweetablesForm
from .forms.clicktotweet import ClickToTweetForm
from .forms.schedule import ScheduleForm
from .forms.wordpress_account import WordpressAccountForm
from .forms.wordpress_account_setup import WordpressAccountSetupForm
from .forms.wordpress_account_sortable import WordpressAccountSortableForm
from .forms.soundcloud_account import SoundcloudAccountForm
from .forms.twitter_account import TwitterAccountForm
from producer.helpers import get_podfunnel_client_and_podcast_for_user
from producer.helpers.soundcloud_api import SoundcloudAPI
from producer.helpers.twitter import TwitterAPI
from django.conf import settings
from producer.models import Client, Production, ChapterMark, ProductionLink, ProductionTweet, Podcast, WordpressConfig, Credentials, WordPressSortableSection, \
TwitterConfig, SoundcloudConfig
from django.core.urlresolvers import reverse
from producer.tasks.auphonic import update_or_create_preset_for_podcast
class EpisodeChapterMarksView(LoginRequiredMixin, View):
form_class = EpisodeChapterMarksForm
template_name = 'fc/forms_chapter_marks.html'
def get(self, request, *args, **kwargs):
initial_values = {}
user = request.user
# Lets get client and podcast for the user already. if not existent raise 404
client, podcast = get_fc_client_and_podcast_for_user(user)
if client is None or podcast is None:
raise Http404
# The production_id or the chaptermark_id must be passed on teh KWargs
production_id = kwargs.get('production_id', None)
chaptermark_id = kwargs.get('chaptermark_id', None)
if chaptermark_id:
chaptermark = get_object_or_404(ChapterMark, id=chaptermark_id)
production = chaptermark.production
elif production_id:
production = get_object_or_404(Production, id=production_id)
chaptermark = None
initial_values['production_id'] = production.id
if chaptermark is not None:
initial_values['chaptermark_id'] = chaptermark_id
initial_values['start_time'] = chaptermark.start_time
initial_values['title'] = chaptermark.title
form = self.form_class(initial=initial_values)
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
# lets get the data
production_id = form.cleaned_data.get('production_id')
chaptermark_id = form.cleaned_data.get('chaptermark_id')
start_time = form.cleaned_data.get('start_time')
title = form.cleaned_data.get('title')
# Get production
production = get_object_or_404(Production, id=production_id)
# if a chaptermark existed, we update, if not we create
if chaptermark_id is not None:
chaptermark = ChapterMark.objects.get(id=chaptermark_id)
else:
chaptermark = ChapterMark()
chaptermark.start_time = start_time
chaptermark.title = title
chaptermark.production = production
chaptermark.save()
return HttpResponseRedirect(reverse('fc:episodeshowlinks'))
return render(request, self.template_name, {'form': form})
chaptermark.py form:
from django import forms
class EpisodeChapterMarksForm(forms.Form):
production_id = forms.IntegerField(widget=forms.Field.hidden_widget, required=False)
chaptermark_id = forms.IntegerField(widget=forms.Field.hidden_widget, required=False)
start_time = forms.TimeField(required=False)
title = forms.CharField(max_length=200)
chaptermark template:
{% extends "fc/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="progress">
<div class="progress-bar progress-bar-striped progress-bar-success active" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width: 50%">
<span class="sr-only">50% Complete</span>
</div>
</div>
<div class="panel panel-default box-shadow--16dp col-sm-6 col-sm-offset-3">
<div class="panel-body">
<div class='row'>
<div class='col-sm-12'>
{% if title %}
<h1 class='{% if title_align_center %}text-align-center{% endif %}'>{{ title }}<!-- : {{ get.clientsetup.company_name }} --></h1>
{% endif %}
{% if subtitle %}
<h3 class='{% if subtitle_align_center %}text-align-center{% endif %}'>{{ subtitle }}</h4>
{% endif %}
<h5>Chapter Marks</h5>
<form method='POST' action=''>{% csrf_token %}
{{ form|crispy }}
<hr/>
<button type="submit" class="btn btn-primary box-shadow--6dp"><i class="fa fa-chevron-right pull-right"></i> Continue
</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
----------------------UPDATE-------------------------
Was in views.py:
#login_required
def episodechaptermarks(request):
title = 'Podcast'
title_align_center = True
subtitle = 'Setup | Add Episode'
subtitle_align_center = True
form = ChapterMarksForm(request.POST or None)
context = {
"title": title,
"subtitle": subtitle,
"form": form
}
if form.is_valid():
instance = form.save(commit=False)
start_time = form.cleaned_data.get("start_time")
title = form.cleaned_data.get("title")
instance.start_time = start_time
instance.title = title
instance.user = request.user
instance.save()
return render(request, "pod_funnel/forms_chapter_marks.html", context)
else:
return render(request, "pod_funnel/forms_chapter_marks.html", context)
ModelForm:
from django import forms
from producer.models import ChapterMark
class ChapterMarksForm(forms.ModelForm):
class Meta:
model = ChapterMark
fields = ['start_time', 'title']
def clean_start_time(self):
start_time = self.cleaned_data.get('start_time')
return start_time
def clean_title(self):
title = self.cleaned_data.get('title')
return title
In essence, your production object has a series of timestamps that relate back via a FK. You need a set of views for CRUD at the production level. Let's assume your models are already created. There's a few things from my experience I want to point out that I think will point you in the right direction.
Unless absolutely necessary never use a Form class when creating a form object that mirrors a model; you are introducing a need for unnecessary complexity and opening the door for errors. Use a ModelForm, which can save objects to the DB straight from the view and help you manage cleaning, validation, and more. In addition, these can easily mesh with generic views of all sorts.
For this sort of relation (a model object with a varying number of model objects of a given type relating back that object) Django provides the powerful but difficult inlineformset_factory. This creates a series of inline forms as needed for a relation such as this.
So you have a model (production) and another related back to that (timestamp). You need to save these at the same time, possibly perform cleaning or validation, and really provide CRUD functionality for this relationship as a whole. For this, you could create a complex view from scratch or you could use django-extra-views and their generic CBVs for models with inlines. You can subclass CreateWithInlinesView, UpdateWithInlinesView. Why? Most Django devs would agree formsets are difficult to implement.
So, to give you a simplified version of how you can do this
from extra_views.advanced import CreateWithInlinesView, InlineFormSet, UpdateWithInlinesView
class TimeStampsInline(InlineFormSet):
model = models.TimeStamp
form = TimeStampForm # If you haven't created a custom ModelForm, can also specify "fields= ['field_1','field_2',...] and the CBV will create a ModelForm
extra = 0
class ProductionCreate(CreateWithInlinesView):
model=models.Production
inlines = [TimeStampsInline]
success_url = reverse('production-list') # the url to return to on successful create
exclude = ['created_by'] # render all fields except this in form
template_name = 'myapp/update.html'
class ProductionUpdate(UpdateWithInlinesView):
model=models.Production
inlines = [TimeStampsInline]
success_url = reverse('production-list')
exclude = ['created_by']
template_name = 'myapp/update.html'
Your template(s) will have to be built in specification with formsets; there's documentation and tutorials all over for that.
That's already a lot to digest, but you probably get the general idea. Don't build a horse from scratch ;)