Hi all i want make create dynamic model(forms)
Example admin creating new academic year (2019-2020) and this register form is opening(forms/1)
İ want make this system but i dont konow wherede start
İ create acedemic year Model
But how i can check with Createview academicyear url id?
class SınavBasvuruDonemiView(LoginRequiredMixin,CreateView):
model = SinavBasvuruDonem
fields = ['title',"published"]
template_name = "includes/dashboard/addyear.html"
def form_valid(self, form):
article = form.save(commit=False)
article.author = self.request.user
article.save()
messages.add_message(self.request, messages.INFO, 'Makaleniz onaylanınca yayınlanacaktır.')
return redirect("dashboard:addyear")
class BasvuruFormCreateView2(LoginRequiredMixin,CreateView,id):
article = get_object_or_404(Article,id = id)
model = SinavBasvuruDonem
fields = ["grade","student_name","student_surname","student_tc","parent_name","parent_surname","parent_tel","parent_mail"]
template_name = "includes/dashboard/addform.html"
def form_valid(self, form):
article = form.save(commit=False)
article.author = self.request.user
article.save()
messages.add_message(self.request, messages.INFO, 'Makaleniz onaylanınca yayınlanacaktır.')
return redirect("dashboard:addform")
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('academicyear/<int:pk>/create-form/', views.BasvuruFormCreateView.as_view(), name='create-form'),
path('academicyear/create/', views.SınavBasvuruDonemiView.as_view(), name='create-academicyear'),
]
# models.py
from django.db import models
class AcademicYear(models.Model):
title = models.CharField(max_length=100)
published = models.BooleanField(default=False)
class SinavBasvuruDonem(models.Model):
academic_year = models.ForeignKey(AcademicYear, on_delete=models.CASCADE)
grade = models.CharField(max_length=10)
student_name = models.CharField(max_length=100)
# Add other fields
# views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import CreateView
from django.shortcuts import get_object_or_404, redirect
class SınavBasvuruDonemiView(LoginRequiredMixin, CreateView):
model = AcademicYear
fields = ['title', 'published']
template_name = "includes/dashboard/addyear.html"
def form_valid(self, form):
academic_year = form.save(commit=False)
academic_year.save()
messages.add_message(self.request, messages.INFO, 'Akademik yıl oluşturuldu.')
return redirect("dashboard:create-form", pk=academic_year.pk)
class BasvuruFormCreateView(LoginRequiredMixin, CreateView):
model = SinavBasvuruDonem
fields = ["grade", "student_name", "student_surname", "student_tc", "parent_name", "parent_surname", "parent_tel", "parent_mail"]
template_name = "includes/dashboard/addform.html"
def form_valid(self, form):
academic_year_id = self.kwargs['pk']
academic_year = get_object_or_404(AcademicYear, pk=academic_year_id)
form.instance.academic_year = academic_year
form.instance.author = self.request.user
form.save()
messages.add_message(self.request, messages.INFO, 'Başvuru formu oluşturuldu.')
return redirect("dashboard:create-form", pk=academic_year_id)