I have a doctor detail view in which I have passed form as context:
class HospitalDoctorDetailView(DetailView):
context = {}
model = HospitalDoctor
template_name = "hospital_doctor_detail.html"
context_object_name = 'doctor'
def get_context_data(self, **kwargs):
context = super(HospitalDoctorDetailView, self).get_context_data(**kwargs)
context['appointment_form'] = AppointmentForm1
return context
And I have used this form in the template named hospital_doctor_detail.html:
<form action="/appointment/{{doctor.id}}/{{doctor.hospital.id}}/" method="post">{% csrf_token %}
First Name:<br/>
{{ form.first_name.errors }}
{{ appointment_form.first_name }}<br/>
Middle Nmae:<br/>
{{ form.middle_name.errors }}
{{ appointment_form.middle_name }}<br/>
Last Name:<br/>
{{ form.last_name.errors }}
{{appointment_form.last_name}}<br/>
Age:<br/>
{{ form.age.errors }}
{{ appointment_form.age }}<br/>
Date:<br/>
{{ form.appointment_date.errors }}
{{ appointment_form.appointment_date }}<br/>
<br/>
<input type="submit" value="Take Appointment" style="background-color:#EFEFEF; border:1px solid #000;" />
</form>
My form AppointmentForm1 is a model form. To submit a form I have written a view:
def TakeAppointmentView(request, pk, hpk):
doctor = HospitalDoctor.objects.get(pk=pk)
hospital = Hospital.objects.get(pk=hpk)
if request.method == "POST":
form = AppointmentForm1(request.POST)
if form.is_valid():
app = DoctorAppointment()
app.user = request.user
app.doctor = doctor
app.hospital = hospital
app.first_name = form.cleaned_data['first_name']
app.middle_name = form.cleaned_data['middle_name']
app.last_name = form.cleaned_data['last_name']
app.age = form.cleaned_data['age']
app.appointment_date = form.cleaned_data['appointment_date']
app.save()
messages.success(request, "Thank you for taking appointment")
return redirect("doctor_detail", pk)
#return redirect("/home/")
else:
return render_to_response("hospital_doctor_detail.html", {"appointment_form":form}, context_instance=RequestContext(request))
else:
form = AppointmentForm1()
return render_to_response("hospital_doctor_detail.html", {"appointment_form":form}, context_instance=RequestContext(request))
If the form is valid appointment is taken successfully but if the form is invalid I want to redirect it to the same doctor detail view.
Here I am getting an error that if the form is invalid it is redirecting to the doctor detail view with the error message but form is not displayed??
Whats wrong in here??
You're returning your form to your template as {"application_form": form}.
You're template doesn't know anything about any variables called form.
Instead of using:
{{ form.first_name.errors }}
Try using the following for each of your error messages:
{{ application_form.first_name.errors }}
Related
I'm making Django app and I have an issue, I've never had problem with before. As always in form view, I'm checking if request.method == 'POST' but somehow it returns False,
My code looks like that:
def recipe_create_view(request):
context = {}
form = RecipeForm(request.POST or None)
IngredientFormset = formset_factory(IngredientForm)
formset = IngredientFormset(request.POST or None)
context['form'] = form
context['formset'] = formset
if request.method == 'POST':
if form.is_valid():
if formset.is_valid():
form.save()
print("made a recipe")
for form in formset:
child = form.save(commit=False)
child.recipe = parent
child.save()
print("made a Ingredient")
else:
print("formset is not valid")
else:
print("form is not valid")
else:
print("request method is not correct")
return render(request, 'recipes/create_recipe.html', context)
create_recipe.html file:
<form method="POST">
{% csrf_token %}
<label>recipe</label>
<p>{{form}}</p>
<label>ingredients</label>
{% for form in formset %}
<ul>
<label>name</label>
<li>{{ form.name }}</li>
<label>quantity</label>
<li>{{ form.quantity }}</li>
</ul>
{% endfor %}
<div>
<input type="submit" value="submit" class="button-33" role="button">
</div>
</form>
Where is the problem?
It is necessary to return HttpResponseRedirect after dealing with POST data, the tip is not specific to Django, it's a good web practice in general.
Also, try to maintain both GET and POST request separately, so try below view:
def recipe_create_view(request):
context = {}
form="" # for the error of variable refrenced before assignment.
IngredientFormset=""
formset=""
if request.method == 'POST':
form = RecipeForm(request.POST)
IngredientFormset = formset_factory(IngredientForm)
formset = IngredientFormset(request.POST)
if form.is_valid():
if formset.is_valid():
form.save()
print("made a recipe")
for form in formset:
child = form.save(commit=False)
child.recipe = parent
child.save()
print("made a Ingredient")
return redirect('some_success_path_name')
else:
print("formset is not valid")
else:
print("form is not valid")
else: # GET method
print("request method is GET")
form = RecipeForm()
IngredientFormset = formset_factory(IngredientForm)
formset = IngredientFormset()
context['form'] = form
context['formset'] = formset
return render(request, 'recipes/create_recipe.html', context)
add action in your HTML form and POST in small case.
<form action="/your_backend_url_to_view/" method="post">
{% csrf_token %}
<label>recipe</label>
<p>{{form}}</p>
<label>ingredients</label>
{% for form in formset %}
<ul>
<label>name</label>
<li>{{ form.name }}</li>
<label>quantity</label>
<li>{{ form.quantity }}</li>
</ul>
{% endfor %}
<div>
<input type="submit" value="submit" class="button-33" role="button">
</div>
</form>
views.py
#login_required(login_url='/account/login/')
def TaskCreateView(request,pk,todo_id):
if not request.user.is_authenticated:
return redirect('accounts:index')
else:
instance = get_object_or_404(Level, pk=pk)
qs = instance.todo_set.get(id = todo_id)
todo = Task.objects.filter(todo=qs, student=request.user)
if todo.exists():
messages.warning(request, 'You Already Completed This Task')
return HttpResponseRedirect(instance.get_absolute_url())
form = StudentTaskForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.instance.user = User.objects.get(id=request.user.id)
obj = form.save(commit=False)
obj.student = request.user
obj.todo = qs
obj.level = instance
obj.save()
ImageFormSet = modelformset_factory(Images,
form=ImageForm, extra=3)
formset = ImageFormSet(request.POST, request.FILES,
queryset=Images.objects.none())
if request.method == 'POST':
if formset.is_valid():
for form in formset.cleaned_data:
image = form['image']
photo = Images(post=form, image=image)
photo.save()
return redirect('student:dashboard')
return render(request,'task_form.html',
{'form':form,"qs":qs,'formset':formset})
forms.py
class StudentTaskForm(forms.ModelForm):
title = forms.CharField(widget=forms.TextInput(attrs={'class':
'form-control',' type': "text",'placeholder':'Enter Title'}))
content = forms.CharField(widget=SummernoteWidget())
class Meta:
model = Task
fields = [
'title',
'content',
]
widgets = {
'content': SummernoteWidget(),
}
class ImageForm(forms.ModelForm):
image = forms.ImageField(label='Image')
class Meta:
model = Images
fields = ('image', )
I have two models Task and Images and I'm using two forms for the same.
Im trying to implement multiple image upload for the same. When I try to load the form I'm encountering this error. I have added the {{ formset.management_form }} in the template. The images model has a foreign key to the Task.
template:
<form id="post_form" action="" method="post"
enctype="multipart/form-data">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form %}
{{ field }} <br />
{% endfor %}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
<div class="panel-body">
<button type="submit" class="btn btn-primary m-t-10">Submit</button>
</div>
</form>
You should only instantiate the formset with request.POST for POST requests.
if request.method == 'POST':
formset = ImageFormSet(request.POST, request.FILES,
queryset=Images.objects.none())
...
else:
# GET request
formset = ImageFormSet(queryset=Images.objects.none())
...
Using flask and WTForms. I am trying to combine my edit and create forms for posts. When a user clicks edit, the view checks if there is a post id passed, if there is it grabs the current info for that post and populates the form in the template. I can get this to work for every field but the 'main_post' field which is radio buttons.
View:
#app.route('/posts-update' , methods=['POST','GET'])
#login_required
def posts_update():
form = forms.Post()
if request.method == 'POST' and request.form['id']:
post = Post.query.filter_by(id=request.form['id']).first()
form.title.data = post.title
form.body.data = post.body
# Works for all but main_post
form.main_post.data = post.main_post
# Also tried this and it didn't work
# form = forms.Post(obj=post)
else:
post = False
# form = forms.Post()
return render_template('post_update.html', post=post, form=form)
Form:
#post create and update
class Post(Form):
title = StringField('Title', validators=[DataRequired()])
body = TextAreaField('Body', validators=[DataRequired()])
main_post = RadioField('Make Main Post', choices=[('1', 'yes'), ('0', 'no')], validators=[DataRequired()])
Template:
<input type="hidden" name="id" {% if post.id %}value="{{ post.id }}"{% endif %}>
{{ form.title.label }} : {{ form.title }} <br/>
{{ form.body.label }} : {{ form.body }} <br/>
{{ form.main_post.label }} : {{ form.main_post }} <br/>
<button class="button postfix" type="submit">{% if post == False %}Add{% else %}Update{% endif %}</button>
A similar question was asked and answered here How to dynamically set default value in WTForms RadioField?
You should be able to do this by setting the default value and then running form.process().
form.main_post.default = post.main_post
form.process()
After creating a form and setting required=True the form shows the validation errors immediately when loading the page.
Of course this should only happen after submitting.
How would I be able to make sure the proper errors only show after submitting?
forms.py
class CurrencyConverterForm(forms.Form):
base_currency = forms.ModelChoiceField(queryset=Currency.objects.all(), required=True)
counter_currency = forms.ModelChoiceField(queryset=Currency.objects.all(), required=True)
base_amount = forms.FloatField(required=True)
index.html
<form action="" method="get">
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.base_currency.errors }}
<label for="{{ form.base_currency.id_for_label }}">From Currency</label>
{{ form.base_currency }}
</div>
<div class="fieldWrapper">
{{ form.counter_currency.errors }}
<label for="{{ form.counter_currency.id_for_label }}">To Currency</label>
{{ form.counter_currency }}
</div>
<div class="fieldWrapper">
{{ form.base_amount.errors }}
<label for="{{ form.base_amount.id_for_label }}">Amount</label>
{{ form.base_amount }}
</div>
</form>
views.py
def index(request):
counter_amount = ""
if request.method == 'GET':
form = CurrencyConverterForm(request.GET)
if form.is_valid():
# Get the input data from the form
base_currency = form.cleaned_data['base_currency']
counter_currency = form.cleaned_data['counter_currency']
base_amount = form.cleaned_data['base_amount']
# Calculate the counter_amount
counter_amount = get_conversion_amount(base_currency, counter_currency, datetime.now(), base_amount)
# Retrieve the counter amount from the dict
counter_amount = counter_amount['GetConversionAmountResult']
# Maximize the number of decimals to 4
if counter_amount.as_tuple().exponent < -4:
counter_amount = "%.4f" % counter_amount
else:
form = CurrencyConverterForm()
context = {
'form': form,
'counter_amount': counter_amount
}
return render(request, '../templates/client/index.html', context)
The problem is that both requests are GETs: both the initial request to get the form, and the request to submit the form. So there is no point in checking if request.method == 'GET', because it is always true.
Instead, check that there is actually information in the GET dictionary:
if request.GET:
Note that this will not work if you need to show an error on a completely empty submission, though.
I'm trying to set an home page one my project that show a search form (with GET method).
The problem is that it raises an error at the moment I load the very page since the default request method is GET. Of course I can change the form's method to POST but that would not be the correct way, so I was wondering if there was any condition that I can set to avoid this issue and still be able to check any errors.
I'm working on Django 1.5 and Python 2.7
This if the form class:
class Search(forms.Form):
middleschool = 'MS'
highschool = 'HS'
university = 'U'
blank = '-'
school_choices = ((middleschool, 'Middle School'),
(highschool, 'High school'),
(university, 'University'),
(blank, 'Not defined'),)
title = forms.CharField(label='Keyworld')
subject = forms.ModelChoiceField(queryset=Subject.objects.order_by('?'),
required=False, label='Whitch subject you want to search?')
school = forms.ChoiceField(choices = school_choices, required=False,
label='What level of material are you searching?')
price = forms.BooleanField(required=False)
This is the relative view:
def home(request):
if request.method == 'GET':
form = Search(request.GET)
if form.is_valid():
cd = form.cleaned_data
ftitle = cd['title']
fsubject = cd['subject']
fschool = cd['school']
fprice = cd['price']
if fprice:
forms = File.objects.filter(name__contains='ftitle', subject='fsubject', school='fschool', price = '0,0')
return render(request, 'search.html', {'form': form})
else:
forms = File.objects.filter(name__contains='ftitle', subject='fsubject', school='fschool')
return render(request, 'search.html', {'form': form})
else:
form = Search()
return render(request, 'home.html', {'form': form})
This is the HTML:
{% block content %}
<hr>
<div id='search'>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="/search/" method="get">
<div class="field">{{ form.title.errors }}<label for="keyworld">Keyworld:</label>{{ form.title }}</div>
<div class="field"><label for="subject">Subject:</label>{{ form.subject }}</div>
<div class="field"><label for="school">Level:</label>{{ form.school }}</div>
<div class="field"><label for="price">Price yes or no:</label>{{ form.price }}</div>
<input type="submit" value="Search">
<input type="reset" value="Reset">
</form>
</div>
<hr>
{% endblock %}
You simply have to add or None, like this:
form = SearchForm(request.GET or None)
Rather than checking the request method, you could check whether request.GET is empty:
if request.GET:
form = SearchForm(request.GET)