I want to get the form object from self.Form
This is my form
class ActionLogSearchForm(forms.Form):
key_words = forms.CharField(required=False)
and I set form as form_class, however I can't fetch the form data in view
class ActionLogListView(LoginRequiredMixin, ListSearchView):
template_name = "message_logs/action_log.html"
form_class = ActionLogSearchForm
def get_queryset(self):
res = []
form = self.form ## somehow form is None
print(form.cleaned_data) # error occurs here. 'NoneType' object has no attribute 'cleaned_data'
I think this is the simplest set, but how can I make it work?
Try this:
from django.http import HttpResponseRedirect
from django.shortcuts import render
class ActionLogSearchForm(forms.Form):
key_words = forms.CharField(required=False)
class ActionLogListView(LoginRequiredMixin, ListSearchView, request):
form_class = ActionLogSearchForm
def get_queryset(self, request):
res = []
if request.method == 'POST':
form = self.form(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
return render(request, 'message_logs/action_log.html', {'form': form})
Related
I have a foreign key (zone_set) as a choice field in a form. It should only display current project's zone_set . As you can see, It is not the case since it's displaying a zone_set: I belong to an other project, I should not be displayed here which does not belong to the current project.
Here is my form but it doesn't work.
class ODMatrixForm(forms.ModelForm):
class Meta:
model = ODMatrix
# fields = '__all__'
exclude = ('size', 'locked',)
def __init__(self, current_project=None, *args, **kwargs):
super().__init__(*args, **kwargs)
if current_project:
queryset = ZoneSet.objects.filter(project=current_project)
self.fields['zone_set'].queryset = queryset
The view creating the ODMatrix
def create_od_matrix(request, pk):
"""A roadnetwork depends on a project. It
must be created inside the project"""
current_project = Project.objects.get(id=pk)
form = ODMatrixForm(initial={'project': current_project})
if request.method == 'POST':
print(request)
od_matrix = ODMatrix(project=current_project)
# form = ODMatrixForm(request.POST, instance=od_marix)
form = ODMatrixForm(data=request.POST, instance=od_matrix)
if form.is_valid():
form.save()
messages.success(request, "OD matrix cessfully created")
return redirect('od_matrix_details', od_matrix.pk)
context = {
'project': current_project,
'form': form}
return render(request, 'form.html', context)
You created the __init__ as constructor of the Meta class, this should be a constructor of the ODMatrixForm, so:
class ODMatrixForm(forms.ModelForm):
# 🖟 part of ODMatrixForm, not Meta
def __init__(self, current_project=None, *args, **kwargs):
super().__init__(*args, **kwargs)
if current_project:
self.fields['zone_set'].queryset = ZoneSet.objects.filter(
project=current_project
)
class Meta:
model = ODMatrix
# fields = '__all__'
exclude = ('size', 'locked',)
In the view, you will need to pass the current_project when you construct an ODMatrixForm object:
from django.shortcuts import get_object_or_404
def create_od_matrix(request, pk):
current_project = get_object_or_404(Project, pk=pk)
if request.method == 'POST':
form = ODMatrixForm(current_project, request.POST, request.FILES)
if form.is_valid():
form.instance.project = current_project
form.save()
messages.success(request, 'OD matrix cessfully created')
return redirect('od_matrix_details', od_matrix.pk)
else:
form = ODMatrixForm(current_project=current_project)
context = {
'project': current_project,
'form': form
}
return render(request, 'form.html', context)
Note: It is often better to use get_object_or_404(…) [Django-doc],
then to use .get(…) [Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using
.get(…) will result in a HTTP 500 Server Error.
How can I pass the form with an actual filled profile informations (like ProfileUpdateForm(instance=x) in function views).
I did it this way and have no idea how to pass an actual profile instance to the form. Something like UpdateView, but in my DetailView class
class ProfileDetailView(FormMixin, DetailView):
model = Profile
context_object_name = 'profile'
template_name = 'profiles/myprofile.html'
form_class = ProfileUpdateForm
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy('profiles:profile', kwargs={'pk': self.get_object().pk})
If you want to update an instance of a model you should use UpdateView [Django docs]:
from django.urls import reverse
from django.views.generic.edit import UpdateView
class ProfileDetailView(UpdateView):
model = Profile
context_object_name = 'profile'
template_name = 'profiles/myprofile.html'
form_class = ProfileUpdateForm
def get_success_url(self):
# Use `reverse` instead of `reverse_lazy`. You will get an error otherwise!
return reverse('profiles:profile', kwargs={'pk': self.get_object().pk})
Also if there is any need to pass a keyword argument to the form (here instance, although I would suggest to stick with UpdateView) you should override get_form_kwargs:
class MyView(SomeGenericView):
...
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['some_kwarg'] = 'Some value'
# Your use case implies below line
# kwargs['instance'] = self.get_object()
return kwargs
I used this tutorial to successfully set up a dynamic inline formset using Django. The CreateView works great, but I cannot get the UpdateView to actually update the related fields. There are no errors thrown, but the items will not update. I believe I have isolated the error to the form_valid function. The code is as follows. Thank you for any assistance.
class ApplicantCreate(CreateView):
model = Applicant
success_message = 'Your application was submitted successfully.'
form_class = forms.ApplicantForm
template_name = 'careers/add_applicant.html'
success_url = reverse_lazy('careers:thanks')
def get_context_data(self, **kwargs):
data = super(ApplicantCreate, self).get_context_data(**kwargs)
positions = super(ApplicantCreate, self).get_context_data(**kwargs)
if self.request.POST:
data['employer'] = forms.ApplicantEmployerFormSet(
self.request.POST,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
self.request.POST,
prefix='education')
else:
data['employer'] = forms.ApplicantEmployerFormSet(prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(prefix='education')
return data
context['unfilled_positions'] = Position.objects.filter(filled=False)
return positions
def form_valid(self, form):
context = self.get_context_data()
employer = context['employer']
education = context['education']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if employer.is_valid():
employer.instance = self.object
employer.save()
if education.is_valid():
education.instance = self.object
education.save()
return super(ApplicantCreate, self).form_valid(form)
def get_success_url(self):
return reverse_lazy('careers:thanks')
class ApplicantUpdate(SuccessMessageMixin,LoginRequiredMixin,GroupRequiredMixin,UpdateView):
group_required = [u'careers-admin',u'careers']
model = Applicant
success_message = '%(first_name)s %(last_name)s was updated successfully.'
form_class = forms.ApplicantUpdateForm
template_name = 'careers/edit_applicant.html'
def get_context_data(self, **kwargs):
data = super(ApplicantUpdate, self).get_context_data(**kwargs)
positions = super(ApplicantUpdate, self).get_context_data(**kwargs)
if self.request.POST:
data['employer'] = forms.ApplicantEmployerFormSet(
self.request.POST,
instance=self.object,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
self.request.POST,
instance=self.object,
prefix='education')
else:
data['employer'] = forms.ApplicantEmployerFormSet(
instance=self.object,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
instance=self.object,
prefix='education')
return data
context['unfilled_positions'] = Position.objects.filter(filled=False)
return positions
def form_valid(self, form):
context = self.get_context_data()
employer = context['employer']
education = context['education']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if employer.is_valid():
employer.instance = self.object
employer.save()
if education.is_valid():
education.instance = self.object
education.save()
return super(ApplicantUpdate, self).form_valid(form)
def get_success_url(self):
return reverse_lazy('careers:applicant_detail',kwargs={'pk': self.object.pk})
This is not a good blog post, because no errors are shown if any of the inline forms aren't valid. As a user, I wouldn't expect the view to just silently ignore errors in the inline forms, saving my main instance successfully and not reporting back those errors.
Note that I don't know what the errors are, maybe it's just an issue with the management form. But in any case, formset errors should be handled before the main object is actually saved.
In general, if you need to write a view with multiple forms (including a formset), it's better to use a function-based view or a View where you write the get and post than trying to force this into a generic class-based view, which is not meant for this.
def multiple_forms_view(request, object_id):
# if this is a POST request we need to process the form data
obj = get_object_or_404(MyModel, pk=object_id)
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = MyForm(request.POST, instance=obj)
formset = MyFormSet(request.POST, instance=obj, ...)
# check whether it's valid:
if form.is_valid() and formset.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = MyForm(instance=obj)
formset = MyFormSet(instance=obj, ...)
return render(request, 'name.html', {'form': form, 'formset': formset})
That way your template can render the errors of each form, including those in the formset. As mentioned before, you could also do this in a View (so your mixins will work), just write the get and post methods and return the same as in the function-based view above.
I have a Django Form (ModelForm) which has a number of fields. When the user presses submit these are then saved to a database. What I am struggling to work out is, how do I then output/render these results in some other HTML page.
Models.py
from django.db import models
# Create your models here.
class Contract(models.Model):
name = models.CharField(max_length=200)
doorNo = models.SlugField(max_length=200)
Address = models.TextField(blank=True)
Forms.py
from django import forms
from contracts.models import Contract
class GenerateContract(forms.ModelForm):
class Meta():
model = Contract
fields = '__all__'
Views.py
from django.shortcuts import render
from contracts.forms import GenerateContract
# Create your views here.
def index(request):
return render(request, 'contracts/index.html')
def contractview(request):
form = GenerateContract()
if request.method == "POST":
form = GenerateContract(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
print('ERROR')
return render(request,'contracts/contracts.html',{'form':form})
At the moment, I am returning the 'Index' Home page of the app as a placeholder.
After validation, the form data is found in form.cleaned_data dictionary. So you can pass that back to the template and display it as you see fit.
from django.shortcuts import render
from contracts.forms import GenerateContract
# Create your views here.
def index(request):
return render(request, 'contracts/index.html')
def contractview(request):
form = GenerateContract()
if request.method == "POST":
form = GenerateContract(request.POST)
if form.is_valid():
form.save(commit=True)
return render(request,'contracts/contracts.html',{'form_data': form.cleaned_data})
else:
print('ERROR')
return render(request,'contracts/contracts.html',{'form':form})
If you want to show the form with the saved values, you can render the template with form and fill the instance input . like this:
from django.shortcuts import render
from contracts.forms import GenerateContract
# Create your views here.
def index(request):
return render(request, 'contracts/index.html')
def contractview(request):
form = GenerateContract()
if request.method == "POST":
form = GenerateContract(request.POST)
if form.is_valid():
saved_instance = form.save(commit=True)
return render(request,'contracts/contracts.html',{'form':GenerateContract(instance=saved_instance)})
else:
print('ERROR')
return render(request,'contracts/contracts.html',{'form':form})
How do I pass the object from ClassDetail to CreateNewStudent in order to use it in its template ?
Thanks.
class ClassDetail(DetailView):
context_object_name = "Class"
template_name = "temp/students.html"
model = Class
class CreateNewStudent(CreateView):
model = Student
form_class = forms.StudentForm
template_name = "temp/newstudent.html"
As I see you have two models Class and Student. In this case better way to create custom view:
def myCustomView(request, pk):
get_class = get_object_or_404(Class, pk=pk)
student_form = StudentForm
if request.POST:
student_form = StudentForm(request.POST)
if student_form.is_valid():
...
return ...
else:
return render(request, 'index.html', {'get_class':get_class, 'student_form':sudent_form})
else:
return render(request, 'index.html', {'get_class':get_class, 'student_form':sudent_form})