I was working with normal crispy forms in django. I changed the form to multi step form, now the submit doesnt work. So when i click on submit data doesnt get stored in the db.
This is how the updated template looks like: index.html
index.html
<ul id="progressbar">
<li class="active">Account Setup</li>
<li>Social Profiles</li>
<li>Personal Details</li>
</ul>
<!-- fieldsets -->
<fieldset>
<h2 class="fs-title">Help me know you better</h2>
<h3 class="fs-subtitle">This is step 1</h3>
{{ form.associate_image|as_crispy_field}}
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset>
<h2 class="fs-title">Social Profiles</h2>
<h3 class="fs-subtitle">Your presence on the social network</h3>
{{ form.title|as_crispy_field}}
{{ form.client|as_crispy_field }}
{{ form.lob|as_crispy_field }}
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset>
<h2 class="fs-title">Personal Details</h2>
<h3 class="fs-subtitle">We will never sell it</h3>
{{ form.gender|as_crispy_field }}
{{ form.phone_number|as_crispy_field }}
<textarea name="address" placeholder="Address"></textarea>
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="submit" name="submit" id="submit-award" class="submit action-button" value="Submit" />
</fieldset>
This is the view function :
views.py
#login_required
def save_information(request):
associate_id = id_to_numeric(request.user.username)
ldap = Ldap()
current_user = UserDetails.objects.filter(associate_nbr=associate_id)
if current_user.exists():
current_user.delete()
associate_details = ldap.search(associate_id=associate_id)[0]
details = UserDetails(
associate_name=associate_details['name'],
associate_nbr=associate_id,
associate_email=associate_details['email'],
associate_department_id=id_to_numeric(associate_details['department']),
associate_mgr=associate_details['xxx'],
associate_exec=associate_details['xxx'],
associate_org=associate_details['org'],
associate_image=request.FILES['associate_image'],
gender=request.POST['gender'],
title=request.POST['title'],
client=request.POST['client'],
lob=request.POST['lob'],
phone_number=request.POST['phone_number'] ,
)
details.save()
return redirect('/?submit=true')
Related
In my case, I want to access the context value which is present in the outer for loop and use(print) that value in the inner for loop section. There are two context values one for the outer loop and the other one is for the inner loop.
here is my template logic. when the condition becomes false it did not print the values that belong to the outer-loop context variable(else part of if statement not working).
<div class="panel-group" id="accordion">
{% for x in queryset %}<!--outerloop-->
<div id="collapse{{ forloop.counter }}" class="panel-collapse collapse">
<div class="panel-body">
<div>
<form action="tobeupdated" method="POST" enctype="multipart/form-data" >
{% csrf_token %}
<label>Q- <input type="text" id="ques" name="ques" value="{{x.question.question}}" readonly></label><br><br>
<img src="{{x.question.image}}" alt="" srcset="" width="700"><br><br>
{% for q2 in queryset2 %}<!--innerloop-->
{% if q2.option_id == x.option_id %}
<label>A- <input type="text" id="op1" name="op1" value="{{q2.option.option1}}" readonly><br><br>
<img src="{{q2.option1_img}}" alt="" srcset="" width="400"><br><br>
<label>B- <input type="text" id="op2" name="op2" value="{{q2.option.option2}}" readonly><br><br>
<img src="{{q2.option2_img}}" alt="" srcset="" width="400"><br><br>
<label>C- <input type="text" id="op3" name="op3" value="{{q2.option.option3}}" readonly><br><br>
<img src="{{q2.option3_img}}" alt="" srcset="" width="400"><br><br>
<label>D- <input type="text" id="op4" name="op4" value="{{q2.option.option4}}" readonly><br><br>
<img src="{{q2.option4_img}}" alt="" srcset="" width="400"><br><br>
{% else %}
<label>A- <input type="text" id="op1" name="op1" value="{{x.option.option1}}" readonly><br><br>
<label>B- <input type="text" id="op2" name="op2" value="{{x.option.option2}}" readonly><br><br>
<label>C- <input type="text" id="op3" name="op3" value="{{x.option.option3}}" readonly><br><br>
<label>D- <input type="text" id="op4" name="op4" value="{{x.option.option4}}" readonly><br><br>
{% endif %}
{% endfor %}
<label>Correct Ans- <input type="text" id="ans" name="ans" value="{{x.correct_ans}}" readonly><br><br>
<input type="hidden" id="dlevel" name="dlevel" value="{{x.question.difficulty}}">
<input type="checkbox" name="" checked>
<label>{{x.question.difficulty}}</label>
<!-- <input type="submit" id="update" value="Update" /> -->
<button type="submit" class="btn btn-primary" name="qid" value="{{x.question.id}}" >Edit</button>
<button type="submit" class="btn btn-primary" name="delete_qid" value="{{x.question.id}}" >Delete</button>
</form>
</div>
<!--Toogle Button-->
</div>
</div>
{{x.question.id}}
<!-- Button trigger modal -->
</div>
</div>
{% endfor %}
</div>
Any suggestions regarding this problem will be highly appreciated.
I'm using a FormSet with crispy, and have but a submit button on each row, however hitting submit does not update records currently.
I've searched and found some similar answers which suggest the submit isn't inside the form, but mine are. Also that a form action is missing, but none of my other crispy forms have actions and they work without issue.
Are there any other reasons seen from the code below that would cause the records to not save?
forms.py
class ChangeGroupForm(FormHelper):
def __init__(self, *args, **kwargs):
super(ChangeGroupForm, self).__init__(*args, **kwargs)
self.form_method = 'post'
self.css_class = 'form-inline'
self.form_id = 'changegroup_form'
self.form_show_labels = False
self.layout = Layout(
Div(
Div(
Div(
Field('group', placeholder='Group', css_class="form-control mb-2 mr-sm-2"),
css_class='col-lg-3'
),
Div(
Field('gps', placeholder='gps coords', css_class="form-control mb-2 mr-sm-2"),
css_class='col-lg-8'
),
Div(
HTML("""<input type="submit" name="submit" value="Save" class="btn btn-primary mt-1"/>"""),
css_class='col-lg-1'
),
css_class='row'
),
)
)
self.render_required_fields = True
views.py
#login_required
def db_change_groups(request):
change_form = modelformset_factory(ChangeGroup, fields=('group','gps'))
change_form_helper = ChangeGroupForm()
return render(request, 'home/db_change_groups.html', {
"change_form": change_form,
"change_form_helper": change_form_helper,
})
template.html
{% crispy change_form change_form_helper %}
rendered html
<form id="changegroup_form" method="post"> <input type="hidden" name="csrfmiddlewaretoken"
value="7v0000CPl3G70M6HLfF2FAiwefdfsdgdfwewdf7Gp4nay1hFqZ1Y34SBUA000mHBZQ54">
<div> <input type="hidden" name="form-TOTAL_FORMS" value="10" id="id_form-TOTAL_FORMS"> <input type="hidden"
name="form-INITIAL_FORMS" value="9" id="id_form-INITIAL_FORMS"> <input type="hidden"
name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS"> <input type="hidden"
name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS"> </div>
<div>
<div class="row">
<div class="col-lg-3">
<div id="div_id_form-0-group" class="form-group">
<div class="controls "> <input type="text" name="form-0-group" value="A" maxlength="50"
class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="Group"
id="id_form-0-group"> </div>
</div>
</div>
<div class="col-lg-8">
<div id="div_id_form-0-gps" class="form-group">
<div class="controls "> <input type="text" name="form-0-gps" maxlength="255"
class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="gps coords"
id="id_form-0-gps"> </div>
</div>
</div>
<div class="col-lg-1"> <input type="submit" name="submit" value="Save" class="btn btn-primary mt-1" />
</div>
</div>
</div> <input type="hidden" name="form-0-id" value="1" id="id_form-0-id">
<div>
<div class="row">
<div class="col-lg-3">
<div id="div_id_form-1-group" class="form-group">
<div class="controls "> <input type="text" name="form-1-group" value="B" maxlength="50"
class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="Group"
id="id_form-1-group"> </div>
</div>
</div>
<div class="col-lg-8">
<div id="div_id_form-1-gps" class="form-group">
<div class="controls "> <input type="text" name="form-1-gps" maxlength="255"
class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="gps coords"
id="id_form-1-gps"> </div>
</div>
</div>
<div class="col-lg-1"> <input type="submit" name="submit" value="Save" class="btn btn-primary mt-1" />
</div>
</div>
</div>
</div> <input type="hidden" name="form-9-id" id="id_form-9-id">
</form>
You need to attach somthing like this to your view
def db_change_groups(request):
....
if request.method == "POST":
form = ChangeGroupForm(request.POST)
if form.is_valid():
# Access cleaned data with
group = form.cleaned_data['group']
# Then you can save this to a model.
# return success template or something
else:
# Check for errors.
If you created the form with ModelForm instead of FormHelper you could use form.save() which you automatically save it to the model.
I have a custom page which calls allauth signup view (custom_passcheck.html)
<form action="{% url 'account_signup' %}" method="post" class="form">
{% csrf_token %}
<BR>
<BR>
<BR>
<center>
<div class=signuponepage>
<div class=sign-up>
<div class=signup-box>
<div class="form-element email-address">
<label id="email-address-label">
<strong>
Create a password (it must be 6 characters long)
</strong>
<input type="hidden" name="username" value="{{ pemail }}">
<input type="hidden" name="email" value="{{ email }}">
<input type="text" maxlength="19" name="password1" value="" placeholder="Password">
<input type="text" maxlength="19" name="password2" value="" placeholder="Password (again)">
</label>
</div>
<div class="form-element nextstep-button">
{% buttons %}
<button type="submit" class="btn btn-primary btn-sm">
Finish {% bootstrap_icon "chevron-right" %}
</button>
{% endbuttons %}
</div>
</div>
</div>
</div>
</center>
</form>
Now, if a user puts in password less than 6 chars, it will raise an error and redirect the user to allauth signup page (/account/signup)
Question: instead of redirecting to (/account/signup), how can i redirect to the custom page (custom_passcheck.html) while preserving POST data
<tbody>
{% for sec in sec_list %}
<tr>
<td>{{sec.c_id_id}}.{{sec.sec_id}}</td>
<td>{{sec.title}}</td>
<td>{{sec.name}}</td>
<td>{{sec.time}}</td>
<td>{{sec.r_no_id}}</td>
<td>{{sec.cur}}/{{sec.capcity}}</td>
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="submit" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="submit" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="submit" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>
</tr>
{% endfor %}
</tbody>
I want to use a hidden form to transfer some parameters in a table.My code is above.But when I use
request.POST.get("limit",'')
,I got a null one.How to fix it
edit your html code by replacing the type="submit" to type="hidden",
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="hidden" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="hidden" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>
Shouldn't those inputs be type=hidden instead of class=hidden?
<input type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
The type=submit in all of them makes no sense to me as it is supposed to be used only in submit buttons.
I'm trying to enter the page while I'm not logged in and app redirect me to login page and getting the next parameter is working (ex.
localhost:8000/login/?next=/blog/add/
but when I enter login and password and click login button it redirect me to home page, not that one that is in the next parameter
localhost:8000, not localhost:8000/blog/add/
I'm using default django login view so it is possible to do this without changing anything in login view?
Everything is default by django, I added only next parameter while redirecting to login page (with using #login_required(login_url='login')), I heard that Django has built in feature for redirecting after login with next parameter and nothing more is required?
urls.py
url(r'^login/$',
django.contrib.auth.views.login,
{
'template_name': 'user/login.html',
'authentication_form': user.forms.BootstrapAuthenticationForm,
'extra_context':
{
'title': 'Logowanie',
'year': datetime.now().year,
}
},
name='login'),
login.html
<form action="" method="post" class="form-horizontal">
{% csrf_token %}
<h4>Uzyj konta LDAP zeby sie zalogowac</h4>
<hr />
<div class="form-group">
<div class="col-md-10">
{{ form.username }}
</div>
</div>
<div class="form-group">
<div class="col-md-10">
{{ form.password }}
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="hidden" name="next" value="/" />
<input type="submit" value="Zaloguj" class="btn btn-primary btn-block btn-pb" />
</div>
</div>
{% if form.errors %}
<p class="validation-summary-errors">Please enter a correct user name and password.</p>
{% endif %}
</form>
The problem is that you have hardcoded the next variable as / in your template:
<input type="hidden" name="next" value="/" />
You should change it to use {{ next }}:
<input type="hidden" name="next" value="{{ next }}" />