Issue
I have set up my template by import my form with form.as_p before, and it works fine. However, I need to specify each field, as I have a field that I don't want the user to see (and I need to have it in my django admin so I can't exclude it from the form).
However, when I set-up the form with it's individual fields, and submit it, I don't get redirected, I just get back to the filled out form.
I have set-up a number of print fields, and have also tried to print in form_invalid but nothing shows up, I have no idea what the issue is. Can someone suggest how to fix this or maybe solve it another way?
The form in the template
<form method="post" enctype="multipart/form-data" id="adForm" data-municipalities-url="{% url 'ajax_load_municipalities' %}" data-areas-url="{% url 'ajax_load_areas' %}" novalidate>
{% csrf_token %}
<!-- {{ form.as_p }} -->
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.province.errors }}
{{ form.province.label_tag }}
{{ form.province }}
</div>
<div class="fieldWrapper">
{{ form.municipality.errors }}
{{ form.municipality.label_tag }}
{{ form.municipality }}
</div>
<div class="fieldWrapper">
{{ form.area.errors }}
{{ form.area.label_tag }}
{{ form.area }}
</div>
<div class="fieldWrapper">
{{ form.name.errors }}
{{ form.name.label_tag }}
{{ form.name }}
</div>
<div class="fieldWrapper">
{{ form.age.errors }}
{{ form.age.label_tag }}
{{ form.age }}
</div>
<div class="fieldWrapper">
{{ form.title.errors }}
{{ form.title.label_tag }}
{{ form.title }}
</div>
<div class="fieldWrapper">
{{ form.description.errors }}
{{ form.description.label_tag }}
{{ form.description }}
</div>
<div class="fieldWrapper">
{{ form.days_per_week.errors }}
{{ form.days_per_week.label_tag }}
{{ form.days_per_week }}
</div>
<div class="fieldWrapper">
{{ form.hundras.errors }}
{{ form.hundras.label_tag }}
{{ form.hundras }}
</div>
<div class="fieldWrapper">
{{ form.size_offered.errors }}
{{ form.size_offered.label_tag }}
{{ form.size_offered }}
</div>
<div class="fieldWrapper">
{{ form.image1.errors }}
{{ form.image1.label_tag }}
{{ form.image1 }}
</div>
<div class="fieldWrapper">
{{ form.image2.errors }}
{{ form.image2.label_tag }}
{{ form.image2 }}
</div>
<div class="fieldWrapper">
{{ form.image3.errors }}
{{ form.image3.label_tag }}
{{ form.image3 }}
</div>
<div class="fieldWrapper">
{{ form.payment_type.errors }}
{{ form.payment_type.label_tag }}
{{ form.payment_type }}
</div>
<button type="submit">Publicera annons</button>
</form>
The view
class NewAdTakeMyDog(CreateView):
model = Advertisement
form_class = NewAdTakeMyDogForm
template_name = 'core/advertisement_form_take.html'
success_url = reverse_lazy('view_ads_take_my_dog')
def __init__(self):
print('init')
self.pk = None
def form_valid(self, form):
print('Form valid')
form.instance.author = self.request.user
form.instance.is_offering_own_dog = True
form.instance.is_published = False
response = super().form_valid(form)
return response
def get_success_url(self):
print('Get success url')
if self.object.payment_type == 'S':
return reverse('swish_payment_template', kwargs={'pk': self.object.pk})
if self.object.payment_type == 'B':
return reverse('bg_payment', kwargs={'pk': self.object.pk})
forms.py
class NewAdTakeMyDogForm(forms.ModelForm):
CHOICES = [('S',f'Swish - {PRICE_SWISH}'),('B',f'Bankgiro - {PRICE_BANKGIRO}')]
payment_type = forms.CharField(label='Betalsätt', widget=forms.RadioSelect(choices=CHOICES))
hundras = forms.ModelChoiceField(
queryset=DogBreeds.objects.all(),
widget=autocomplete.ModelSelect2(url='breed-autocomplete')
)
class Meta:
model = Advertisement
fields = ('author', 'province', 'municipality', 'area', 'title', 'name', 'age', 'description', 'days_per_week', 'size_offered', 'hundras', 'image1', 'image2', 'image3', 'payment_type')
def __init__(self, *args, **kwargs):
super(NewAdTakeMyDogForm, self).__init__(*args, **kwargs)
self.fields['municipality'].queryset = Municipality.objects.none()
self.fields['area'].queryset = Area.objects.none()
self.fields['area'].required = False
if 'province' in self.data:
try:
# Set municipality queryset
province_id = int(self.data.get('province'))
self.fields['municipality'].queryset = Municipality.objects.filter(province_id=province_id).order_by('name')
# Set area queryset
municipality_id = int(self.data.get('municipality'))
self.fields['area'].queryset = Area.objects.filter(municipality_id=municipality_id).order_by('name')
except (ValueError, TypeError) as e:
pass # invalid input from the client; ignore and fallback to empty Municipality/Area queryset
Solution
I ended up solving the issue by creating an additional form, which I use for the Admin interface.
Also I got crispy forms to work by using:
{{form|crispy}}
instead of:
{% crispy form %}
Answered By - Jhnsbrst
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.