I have a Django ModelForm with some initial data passed to it. Which is working fine so far.
But, if the user doesn't fill in all data, or makes another mistake the initial value will not be looked up again on redisplaying the form.
Here's a piece of code:
class TrainingAddForm(forms.ModelForm):
class Meta:
model = TrainingTasks
fields = ('task','ac_reg','date','wo_no')
def __init__(self, *args, **kwargs):
super(TrainingAddForm, self).__init__(*args, **kwargs)
self.fields['task'].required = False
self.fields['task'].widget.attrs['disabled'] = 'disabled'
self.fields['date'].widget = widgets.AdminDateWidget()
def clean_task(self):
return
An in forms.py:
def add_trainingtask(request, task_id):
if request.POST:
form = TrainingAddForm(request.POST)
if form.is_valid():
tt = TrainingTasks(
trainee = request.user,
task = Tasks.objects.get(pk=task_id),
date = form.cleaned_data['date'],
ac_reg = form.cleaned_data['ac_reg'],
wo_no = form.cleaned_data['wo_no'],
)
tt.save()
return HttpResponseRedirect('/admin/tot/tasks/')
else:
form = TrainingAddForm(initial = {"task": task_id})
return render_to_response('admin/tot/trainingtasks/add.html', {
'form': form,
'task_id': task_id
},
context_instance = RequestContext(request)
)
If a user misses to fill in i.e. the date (which is mandatory) the form will be redisplayed showing an error (field required), but the underlying record of task_id is not shown anymore.
The ID is still there and it's also possible to save the record (after correcting the error), so that's almost an irritating error for the user.
I guess I missed some piece of code, but I can't figure it out.
I'm not sure I understand the logic of your form or your view.
You've included the task field, yet disabled the field. With the task field disabled, the value isn't going to be in the request.POST collection.
In your view, you're passing the form the task_id parameter as the initial data, and if the request.method is a POST, you're retrieving the Task object from the database.
It seems like the Task is something that you want to assign to TrainingTask, but not it's not necessarily something you want to include in the form. Given that, I would:
#forms.py
class TrainingAddForm(forms.ModelForm):
class Meta:
model = TrainingTasks
fields = ('ac_reg','date','wo_no',)
#not include the task
def __init__(self, *args, **kwargs):
super(TrainingAddForm, self).__init__(*args, **kwargs)
self.fields['date'].widget = widgets.AdminDateWidget()
#views.py
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render
from your_app.forms import TrainingAddForm
from your_app.models import Task, TrainingTasks
def add_trainingtask(request, task_id):
#make sure we have a valid Task object, or redirect.
#you could also use a try/except Task.DoesNotExist and show an error
task = get_object_or_404(Task, pk=task_id)
form = TrainingAddForm(request.POST or None)
if request.POST:
if form.is_valid():
tt = TrainingTasks(
trainee = request.user,
task = task,
date = form.cleaned_data['date'],
ac_reg = form.cleaned_data['ac_reg'],
wo_no = form.cleaned_data['wo_no'],
)
tt.save()
#dont hard-code the url here
return HttpResponseRedirect(reverse('admin_tot_tasks'))
return render(request, 'admin/tot/trainingtasks/add.html', {'form': form,
'task' : task})
Hope that helps you out.
Related
I have a detail view with 2 forms and here I provide code for only one of them. The form is located in a modal on user detailed view and I need to redirect the client to that detail view in which the form is. In the post method the request.GET['user'] returns the user id so I have everything needed to achieve this. I have tried the reverse and redirect, nothing worked I guess because of wrong code.
Should I provide a get_success_url() to do that? I think it will cause some problems because I can get user id only from post method.
class UserDetailView(LoginRequiredMixin, DetailView):
model = TbUser
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['entrance_rights_form'] = TbPeopleEntranceRightForm(
user=self.object, initial={'user': self.object})
return context
class TbPeopleEntranceRightFormView(FormView):
form_class = TbPeopleEntranceRightForm
template_name = 'users/create_entrance_permission_modal.html'
def post(self, request, *args, **kwargs):
print(request.POST['user']) # returns user id
entrance_rights_form = self.form_class(
user=None, data=request.POST)
terminal_permissions_form = TbTerminalPermissionForm(user=None)
if entrance_rights_form.is_valid():
entrance_rights_form.save()
return redirect('user-detail', args=(request.POST['user'],))
else:
return redirect('users-list')
urlpatterns = [
path('users-list/', UsersListView.as_view(), name='users-list'),
path('user-detail/<str:pk>/',
UserDetailView.as_view(), name='user-detail'),
path('tb-entrance-right-form/submit',
TbPeopleEntranceRightFormView.as_view(), name='tb-entrance-right-form'),
]
You don't need to pass the user id in the args as a tuple with redirect.
This should work:
if entrance_rights_form.is_valid():
entrance_rights_form.save()
user_id = request.POST['user'] # i suppose this returns user id as you mentioned
return redirect('user-detail', user_id)
EDIT:
you are not rendering the template inside the post method.
def post(self, request, *args, **kwargs):
print(request.POST['user']) # returns user id
entrance_rights_form = self.form_class(
user=None, data=request.POST)
terminal_permissions_form = TbTerminalPermissionForm(user=None)
if entrance_rights_form.is_valid():
entrance_rights_form.save()
return redirect('user-detail', request.POST['user'])
else:
return redirect('users-list')
return render(request, self.template_name, {'form': entrance_rights_form})
So basic task is: add the template name and text to the choices in the ChoiceField with Selector widget to the form using the data from dB linked to the authenticated User only. Template is a model linked to user as a ForeignKey.
I'd like to access the request data(user) via the Form class linked to the connected view as django.views.generic.View class.
I've checked similar questions here:
Curious about get_form_kwargs in FormView
and here: Sending request.user object to ModelForm from class based generic view in Django
and here: Django: Accessing request in forms.py clean function
However they don't touch base with non-FormView classes. And as it's fairly old solutions, I was curious if there's more likely approach to reach request from forms.Form class.
Here's my code:
views.py
class InformFill(View):
form_class = InformForm
temlate_name = 'distrib_db/inform_fill.html'
def get(self, request):
if request.user.is_authenticated():
form = self.form_class(None)
return render(request, self.temlate_name, context={'form': form})
else:
return redirect('distrib_db:login')
def post(self, request):
if request.user.is_authenticated():
form = self.form_class(request.POST)
if form.is_valid():
inform = Inform(flt_numbr=form.cleaned_data['flight_number'], date=form.cleaned_data['date'],
template=form.cleaned_data['text'], user=request.user)
inform.save()
date = form.cleaned_data['date']
flt_numbr = form.cleaned_data['flight_number']
try:
emails, contacts = get_mail_cnt(date, flt_numbr)
# inform = get_object_or_404(Inform, pk=request['pk'])
paxdata = PaxData(inform=inform, emails=' '.join(emails), contacts=' '.join(contacts))
paxdata.save()
return redirect('/inform/{0}/'.format(inform.pk))
# 'distrib_db:detail', context={'pk': inform.id}
except Exception as e:
return render(request, 'distrib_db/sample.html',
context={'date': date, 'flight_number': flt_numbr, 'error': e})
# return render(request, 'distrib_db/sample.html', context={'date': date, 'flt_numbr': flt_numbr})
return render(request, self.temlate_name, context={'form': form})
else:
return redirect('distrib_db:login')
forms.py
class InformForm(forms.Form):
flight_number = forms.CharField(5, widget=forms.TextInput())
date = forms.DateField(widget=forms.DateInput(attrs={'class': 'datepicker'}))
template = forms.ChoiceField(choices=templates, widget=forms.Select(attrs={'id': 'select_box',
'onchange': 'javascript:changer();'}))
text = forms.CharField(widget=forms.Textarea(attrs={'id': 'txt_box', 'class': 'latin',
'maxlength': "160", 'onchange': 'javascript:validateTextArea();'}))
Generally speaking i'd like to achieve smth like this:
`class InformForm(forms.Form):
def get_template_choices(self):
templates = self.request.user.template_set.all()
choices = []
for t in templates:
choices.append((t.text, t.name))
return choices
flight_number = forms.CharField(5, widget=forms.TextInput())
date = forms.DateField(widget=forms.DateInput(attrs={'class':
'datepicker'}))
template = forms.ChoiceField(choices=get_template_choices(),
widget=forms.Select(attrs=
{'id': 'select_box',
'onchange': 'javascript:changer();'}))
text = forms.CharField(widget=forms.Textarea(attrs={'id': 'txt_box',
'class': 'latin',
'maxlength': "160",
'onchange': 'javascript:validateTextArea();'}))`
I'd appreciate any approach, mb I lack knowledge and asking newbie questions, sorry about it.
I just wanna get the python-way solution rather then build some js/jinja walk-arounds.
Thank you for your time!
#
After #Danielius comments, I've made some adjustments:
`class InformForm(forms.Form):
def __init__(self, user=None, *args, **kwargs):
if user:
self.fields['template'] = forms.ChoiceField(choices=tuple([(template.text, template.name) for template in user.template_set.all()]),
widget=forms.Select(attrs={'id': 'select_box', 'onchange': 'javascript:changer();'}))
flight_number = forms.CharField(5, widget=forms.TextInput())
date = forms.DateField(widget=forms.DateInput(attrs={'class': 'datepicker'}))
# template = forms.ChoiceField(choices=templates, widget=forms.Select(attrs={'id': 'select_box',
# 'onchange': 'javascript:changer();'}))
text = forms.CharField(widget=forms.Textarea(attrs={'id': 'txt_box', 'class': 'latin',
'maxlength': "160", 'onchange': 'javascript:validateTextArea();'}))`
Got an error AttributeError: 'InformForm' object has no attribute 'fields'
You can pass request to your form by changing your __init__ method like this :
class InformForm(forms.Form):
...
def __init__(self, user=None,*args, **kwargs):
super(InformForm, self).__init__(**kwargs)
if user:
self.fields['somefield'] = forms.ChoiceField()
self.fields['somefield'].widget = forms.Select()
self.fields['somefield'].queryset = Someobject.objects.filter(User=user)
...
If the User is linked to other object in db by Foreign key, then you will get all the values of other object as select items.
Also , when creating form you could pass user like this :
form= InformForm(user=request.user,data=request.POST)
I have a customer table that has an id, name, telephone and a date where the customer was set to inactive (called 'inactive_date').
In my form, I only want a checkbox to appear. If there is a date, then the checkbox is set. If the date is empty, then the checkbox is not set.
The only way I can set the checkbox so far is by doing the following:
class MyCustomerForm(ModelForm):
inactive_checkbox = forms.BooleanField(initial='checked')
I tried to use a clean or an init method to be able to set the checkbox based on a condition, but these attempts down below have been unsuccessful. My form always return the checkbox as unset regardless of the contents of the inactive_date.
class MyCustomerForm(ModelForm):
inactive_checkbox = forms.BooleanField()
def clean(self, *args, **kwargs):
inactive_date = self.cleaned_data['inactive_date']
if (inactive_date != None):
self.fields['inactive_checkbox'] = 'checked'
return self.cleaned_data
def __init__(self, *args, **kwargs):
super(myCustomerForm, self).__init__(*args, **kwargs)
inactive_date = self.cleaned_data['inactive_date']
if (inactive_date != None):
self.fields['inactive_checkbox'] = 'checked'
class Meta:
model = MyCustomer
fields = ['customer_id','customer_name','customer_phone',
'inactive_checkbox']
Any idea what is wrong with my code or is there perhaps a better way to control my inactive_date through a checkbox?
Try with initial:
https://docs.djangoproject.com/en/1.8/ref/forms/api/#dynamic-initial-values
In your view check the state of inactive_date field and then pass the result to the form via initial argument.
Hope it can help.
I wanted to post my new form based on jorlugaqui's answer as a comment, but comments are very limited in terms of formatting and length.
So here's my new form and view based on jorlugaqui's recommendation:
New form:
class MyCustomerForm(ModelForm):
inactive_checkbox = forms.BooleanField(required=False, label="Inactive")
class Meta:
model = MyCustomer
fields = ['customer_id','customer_name','customer_phone']
New view:
def customer_detail(request):
id = request.GET.get('id')
item = MyCustomer.objects.get(customer_id=id)
if request.method == 'POST':
form = MyCustomerForm(request.POST, instance=item, initial={'inactive_checkbox': item.inactive})
if form.is_valid():
inactive = request.POST.get('inactive_checkbox')
modified_item = form.save(commit=False)
if inactive:
if modified_item.inactive == None:
modified_item.inactive = datetime.datetime.now().strftime("%Y-%m-%d")
else:
modified_item.inactive = None
modified_item.save()
else:
form = MyCustomerForm(initial={'inactive_checkbox': item.inactive}, instance=item)
return render(request, 'basic_detail.html', {'id': id, 'form': form})
Create a new widget with the desired behavior. The default CheckboxInput widget (tested in django 3.2) properly checks or unchecks the checkbox based on if the field is a datetime or None. Then, you just override the response returned from user input:
from django.forms.widgets import CheckboxInput
from django.utils import timezone
class DateTimeCheckboxInput(CheckboxInput):
def value_from_datadict(self, data, files, name):
# cast the boolean returned to a timestamp or None
value = super().value_from_datadict(data, files, name)
return timezone.now() if value else None
Then, you can use this widget in any django model form
class MyCustomerForm(ModelForm):
class Meta:
model = MyCustomer
fields = ['inactive_checkbox']
widgets = {
"inactive_checkbox": widgets.DateTimeCheckboxInput,
}
I do not want the logged in user to show up on this ModelMultipleChoiceField in order to restrict themselves from creating a following relationship with themselves? So how do I exclude the logged in user from the queryset, probably an easy fix but I'm new to Django and it has eluded me for a few hours now.
forms.py
class Add_Profile(forms.ModelForm):
def __init__(self,*args, **kwargs): # initializing your form in other words loading it
super(Add_Profile, self).__init__(*args, **kwargs)
user_id = kwargs.pop('user_id') # taking user_id out of the querylist
self.fields['follows'] = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=UserProfile.objects.filter(~Q(id=user_id)))
class Meta:
model = UserProfile
fields = (
'bio',
'follows',
'theme',
'profile_picture',
)
Views.py
#login_required
def edit_profile(request, user_id):
userprofile = UserProfile.objects.get(pk=user_id)
if request.method == 'POST':
edit_profile = Add_Profile(request.POST, request.FILES, instance=userprofile, user_id=request.user.id)
if edit_profile.is_valid():
edit_profile.save()
return redirect('/home/user/{0}/'.format(request.user.username))
else:
print edit_profile.errors
else:
edit_profile = Add_Profile(instance=userprofile, user_id=request.user.id)
return render (request, 'edit.html', {'form': edit_profile,})
Error: init() got an unexpected keyword argument 'user_id'
You can definitely do it using forms.Form instead of forms.ModelForm with something along the lines of this example in the docs:
from django import forms
from django.contrib.auth import get_user_model
class Add_Profile(forms.Form):
follows = forms.ModelMultipleChoiceField(queryset=None)
def __init__(self, user=None, *args, **kwargs):
super(Add_Profile, self).__init__(*args, **kwargs)
if user is not None:
self.fields['follows'].queryset = get_user_model().objects.exclude(pk=user.pk)
else:
self.fields['follows'].queryset = get_user_model.objects.all()
Just pass in the user you wish to exclude when you instantiate the form:
form = Add_Profile() # all users will be present in the dropdown
some_guy = User.objects.get(pk=4)
form = Add_Profile(user=some_guy) # all users except some_guy will be present
Define an __init__ method for the form class. Pass the logged in userid to the form while initializing it, this will work with a model form.
def __init__(self, *args, **kwargs):
user_id = kwargs.pop('user_id')
super(Add_Profile, self).__init__(*args, **kwargs)
self.fields['follows'] = forms.ModelMultipleChoiceField(queryset=UserProfile.objects.filter(~Q(user_id=user_id)))
While initializing your form, you can pass user_id
address_form = Add_Profile(request.POST, user_id=request.user.id)
The following code successfully adds a new ToolCalibration to my database, however it does not save the SerialFormset or PartFormset. I've been staring at this code trying to figure it out for quite some time now, so any and all help would be greatly appreciated. Thanks!
Forms.py
from django.forms import ModelForm
from django.forms.models import inlineformset_factory
from tool_cal.models import ToolCalibration, SerialNumber, PartNumber
class ToolForm(ModelForm):
class Meta:
model = ToolCalibration
SerialFormSet = inlineformset_factory(ToolCalibration, SerialNumber, can_delete=True)
PartFormSet = inlineformset_factory(ToolCalibration, PartNumber, can_delete=True)
Views.py
class ToolCreate(CreateView):
model = ToolCalibration
template_name = "create.html"
form_class = ToolForm
success_url = '/toolcal/success'
def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
serial_form = SerialFormSet(prefix='serial')
part_form = PartFormSet(prefix='part')
return self.render_to_response(
self.get_context_data(form=form,
serial_form=serial_form,
part_form=part_form))
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance and its inline
formsets with the passed POST variables and then checking them for
validity.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
serial_form = SerialFormSet(self.request.POST, prefix='serial')
part_form = PartFormSet(self.request.POST, prefix='part')
if (form.is_valid() and serial_form.is_valid() and
part_form.is_valid()):
return self.form_valid(form, serial_form, part_form)
else:
return self.form_invalid(form, serial_form, part_form)
def form_valid(self, form, serial_form, part_form):
"""
Called if all forms are valid. Creates a ToolCalibration instance along with
associated Serial and Parts and then redirects to a
success page.
"""
self.object = form.save()
serial_form.instance = self.object
serial_form.save()
part_form.instance = self.object
part_form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, serial_form, part_form):
"""
Called if a form is invalid. Re-renders the context data with the
data-filled forms and errors.
"""
return self.render_to_response(
self.get_context_data(form=form,
serial_form=serial_form,
part_form=part_form))
Have you considered using django-extra-views? It contains a quick and easy CBV for dealing with InlineFormSets.
In addition to an InlineFormSetView and GenericInlineFormSetView, they've also got a CreateWithInlinesView that seems to be just what you want. Relevant docs here.
Example:
from extra_views import InlineFormSet, CreateWithInlinesView, UpdateWithInlinesView,
from extra_views.generic import GenericInlineFormSet
from tool_cal.models import ToolCalibration, SerialNumber, PartNumber
class SerialNumberInline(InlineFormSet):
model = SerialNumber
class PartNumberInline(GenericInlineFormSet):
model = PartNumber
class ToolCreateView(CreateWithInlinesView):
model = ToolCalibration
inlines = [SerialNumberInline, PartNumberInline]
def get_success_url(self):
return self.object.get_absolute_url()
Even if you don't want to use them, you could dig through the code and see how they handle it.