Commit 1991c42a authored by Bengfort's avatar Bengfort
Browse files

try simple createForm

parent 595df793
Pipeline #333 failed with stages
in 2 minutes and 10 seconds
......@@ -26,6 +26,7 @@ from django.contrib.auth import views as auth_views
from django.views.generic import TemplateView
from django.views.i18n import JavaScriptCatalog
from subject_management.views import SubjectCreateView
def dummy(request):
return HttpResponse('', status=204)
......@@ -36,6 +37,7 @@ urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name="login.html"), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('ping/', dummy, name='ping'),
path('foo/', SubjectCreateView.as_view(), name='subject-create'),
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
......
{% extends "base.html" %}
{% load i18n bootstrap4 %}
{% block content %}
<form method="post">
{% for error in form.non_field_errors %}
<p class="alert alert-danger" role="alert">{{ error }}</p>
{% endfor %}
{% csrf_token %}
{% bootstrap_form form.forms.subject %}
{% bootstrap_form form.forms.address %}
{% bootstrap_form form.forms.city %}
{% bootstrap_form form.forms.country %}
<input type="submit" class="btn btn-primary" />
</form>
{% endblock %}
from django import forms
from django.shortcuts import redirect
# from django.urls import reverse
from django.views.generic import CreateView
from betterforms.multiform import MultiModelForm
from . import models
class CountryModelForm(forms.ModelForm):
class Meta:
model = models.Country
fields = '__all__'
class CityModelForm(forms.ModelForm):
class Meta:
model = models.City
exclude = ('country',)
class AddressModelForm(forms.ModelForm):
class Meta:
model = models.Address
exclude = ('city',)
class SubjectModelForm(forms.ModelForm):
class Meta:
model = models.Subject
exclude = ('address',)
class FullSubjectForm(MultiModelForm):
form_classes = {
'subject': SubjectModelForm,
'address': AddressModelForm,
'city': CityModelForm,
'country': CountryModelForm,
}
class SubjectCreateView(CreateView):
form_class = FullSubjectForm
# success_url = reverse('index')
success_url = '/'
template_name = 'subject_management/form.html'
def form_valid(self, form):
country = form['country'].save()
city = form['city'].save(commit=False)
city.country = country
city.save()
address = form['address'].save(commit=False)
address.city = city
address.save()
subject = form['subject'].save(commit=False)
subject.address = address
subject.save()
return redirect('/')
......@@ -13,6 +13,7 @@ setup(
install_requires=[
"Django==2.0.4",
"django-auth-ldap==1.4.0",
"django-betterforms==1.1.4",
"django-bootstrap4==0.0.6",
"django-npm==1.0.0",
"psycopg2==2.7.4",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment