When i put { form } in some template it renders this:
<tr><th></th><td><input type="text" name="login" maxlength="10" /></td></tr>
<tr><th></th><td><input type="text" name="email" /></td></tr>
<tr><th></th><td><input type="text" name="password" maxlength="10" /></td></tr>
but what if I do not need tr,th,td,p tags? this is what I want.
<input type="text" name="login" placeholder="Имя пользователя"/>
<input type="email" name="email" placeholder="E-mail"/>
<input type="password" name="password" placeholder="Пароль"/>
What you're currently seeing is the output of form.as_table, which gives you the form rendered as tr elements that you insert into a set of tags. You'll want to check out Customizing the form template in the django docs to make them do exactly what you want them to do. That link walk you through it with examples.
To just get the input widgets, iterate through the form and only insert the field itself:
<form action="" method="get">
{% for field in form %}
{{ field }}
{% endfor %}
</form>
Related
I am trying to save a contact form, but i am not using the django form but instead the input field.
I am getting the name, email address and message body from the html templates using input field like this <input type="text" id="name"> but the form doesn't get any data from the input field, it seems i am missing something but i cannot find out what is happening.
I have tried passing in the name, email and message into id="name" id="email" id="message" but nothing still happends.
This is my code
index.html
<form method="POST">
{% csrf_token %}
<input type="text" id="name" value="" />
<input type="text" id="email" value="" />
<input type="text" id="address" value="" />
<button type="button">Let's Withdraw</button>
</form>
views.py
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
message= request.POST.get("message")
print("Message Sent")
return redirect("core:index")
else:
pass
This is your form
<form method="POST">
{% csrf_token %}
<input type="text" id="name" value="" />
<input type="text" id="email" value="" />
<input type="text" id="address" value="" />
<button type="button">Let's Withdraw</button>
</form>
change to this
<form method="POST">
{% csrf_token %}
<input type="text" id="name" name="name" value="" />
<input type="text" id="email" name="email" value="" />
<input type="text" id="address" name="message" value="" />
<button type="submit">Let's Withdraw</button>
</form>
add the name=" ... " attribute in your input field and the corresponding values e.g name, email, address.
Change your button type to submit as your would be submiting a form
I have 3 form inputs that will be submitted when one master button is clicked to then be passed into a view as the request parameter. I would like to get the values of first_name, last_name and email inside my view using request.get(). When the button is clicked the values inside my form appear as None
HTML:
<div id="form_content">
<form action="" method="post">
<section class="form_inputs">
<label for="first_name">First Name:</label>
<input type="text" id="first_name">
</section>
<section class="form_inputs">
<label for="last_name">Last Name:</label>
<input type="text" id="last_name">
</section>
<section class="form_inputs">
<label for="email">Email:</label>
<input type="text" id="email">
</section>
<input type="submit" value="Submit">
</form>
</div>
views.py
def home(request):
form_response = request.GET.get("form_content")
print(form_response)
context = {"title": "Home"}
return render(request, "myApp/home.html", context)
first you need to add csrf_token in your code for post method and also give a name for each input like this :
<div id="form_content">
<form action="" method="post">
{% csrf_token %}
<section class="form_inputs">
<label for="first_name">First Name:</label>
<input type="text" name="first_name" id="first_name">
</section>
<section class="form_inputs">
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="last_name">
</section>
<section class="form_inputs">
<label for="email">Email:</label>
<input type="text" name="email" id="email">
</section>
<input type="submit" value="Submit">
</form>
</div>
and then:
def home(request):
first_name = request.POST['first_name']
last_name = request.POST['last_name']
email = request.POST['email']
print(first_name, last_name, email)
context = {"title": "Home"}
return render(request, "myApp/home.html", context)
In your input tags, you have not passed the name parameter. Pass the name parameter in your input tags, as Django collects the data from the name tags.
For example, in your case, it must be:
<input type="text" id="id_first_name" name="first_name">
<input type="text" id="id_last_name" name="last_name">
<input type="text" id="id_email" name="email">
I'm a bit confused with all the django stuff and forms, I hope you can help me.
I'm migrating a cmd line app to django, and I think I don't need to mess with models (db) atm. I just would like to pass the parameters filled in a form to a python script that use these fields as parameters. I would like to keep my html code and skip render the form with a python form class since it has a lot of css code inside each one.
At the moment I have this form:
<form class="nobottommargin" id="template-contactform" name="template-contactform" action="{% url 'get_data' %}" method="post">
{% csrf_token %}
<div class="col_half">
<label for="name">Project Name <small>*</small></label>
<input type="text" id="name" name="name" value="{{ current_name }}" class="sm-form-control required" />
</div>
<div class="col_half col_last">
<label for="group">Project Group <small>*</small></label>
<input type="text" id="group" name="group" value="{{ current_group }}" class="required sm-form-control" />
</div>
<div class="clear"></div>
<div class="col_half">
<label for="version">Project Version<small>*</small></label>
<input type="text" id="version" name="version" value="{{ current_version }}" class="required sm-form-control" />
</div>
<div class="col_half col_last">
<label for="pType">Project Type<small>*</small></label>
<select id="pType" name="pType" class="required sm-form-control">
<option value="">-- Select One --</option>
<option value="bundle">Bundle</option>
<option value="multiple">Multiple</option>
<option value="git">Git</option>
</select>
</div>
<div class="col_half">
<label for="pPath">Project path<small>*</small></label>
<input type="text" id="pPath" name="pPath" value="{{ current_path }}" class="required sm-form-control" />
</div>
<div class="clear"></div>
<div class="col_full hidden">
<input type="text" id="template-contactform-botcheck" name="template-contactform-botcheck" value="" class="sm-form-control" />
</div>
<div class="col_full" style="margin: 50px 0 0 0 ">
<button class="button button-3d nomargin" type="submit" id="template-contactform-submit" name="template-contactform-submit" value="submit">Create Project</button>
</div>
</form>
So, I want to figure out what's the best way to write the get_data function. I also need some sort of custom validation about some fields that involves other checks that exceeds the basic checks, such as max_chars on each field. That means I need to create some sort of my own form.is_valid() function.
Hope you can help me!
Regards
please i tried with this How can I build multiple submit buttons django form?
but it doesnt work for me and im new to django programming
my aim is : when the user enter the identifiant when he clicks on the button recherche i want that all information come on the input tags here is my code
views.py:
def modifierEmploye(request):
if request.method=='POST':
emp2=EmployeForm(request.POST)
if emp2.is_valid():
if 'recherche' in request.POST:
return HttpResponse("recherche")
elif 'enregistrer' in request.POST:
return HttpResponse("enregistrer")
else:
emp2=EmployeForm()
return render(request,"GPI/modifierEmploye.html",locals())
html:
<form action="" method="POST">
{% csrf_token %}
<p> <input id="id" maxlength="200" name="id" type="text" placeholder="identifiant" /></p><br>
<p> <input id="id_nom" maxlength="200" name="nom" type="text" placeholder="Nom" /></p><br>
<p><input id="id_prenom" maxlength="200" name="prenom" type="text" placeholder="Prenom"/></p><br>
<p> <input id="id_departement" maxlength="200" name="departement" type="text" placeholder="Département" /></p><br>
<p> <input id="id_poste" maxlength="200" name="poste" type="text" placeholder="Poste" /></p><br>
<p> <input id="id_telephone" maxlength="200" name="telephone" type="text" placeholder="Téléphone" /></p><br>
<p><input id="id_email" maxlength="200" name="email" type="email" placeholder="Email" /></p>
<p> <input id="id" maxlength="200" name="id" type="text" placeholder="Nom" /></p><br>
<br>
<input type="submit" value="Rechercher" name="recherche" />
<input type="submit" value="Enregistrer" name="enregistrer" />
</form>
this program doesn't work, please qomeone to help me
change your template with this, it will work :
<form action="" method="POST">
{% csrf_token %}
{{emp2.as_p}}
<input type="submit" value="Rechercher" name="recherche" />
<input type="submit" value="Enregistrer" name="enregistrer" />
</form>
I'm working on adding subscriptions using django-registration. Right now, I'm trying to create a Paypal form on the registration_complete page.
I have created a session variable as follows:
def user_created(sender, user, request, **kwargs):
form = RegistrationFormEx(data=request.POST)
new_user = User.objects.get(username=request.POST['username'])
digest=hmac.new(str(request.POST['username'])+str(request.POST['password1']), str(request.POST['password1']),hashlib.sha1).hexdigest()
new_profile = UserProfile(user=new_user,api_key=digest)
new_profile.save()
#now add other fields including password hash as well
uid = new_profile.id
#Add username to session to pass it via Paypal later
request.session['username']=request.POST['username']
merchant_profile = MerchantProfile(user_id=uid,
create_time=datetime.datetime.now(),
modified_time=datetime.datetime.now(),
payment_card_id=uid,
current_state=1,
name=request.POST['name'],
)
merchant_profile.save()
return new_user
user_registered.connect(user_created)
My template for the Paypal form is is as follows:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<p>
{% trans "You are now registered. Activation email sent." %}
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="sumit_1349250468_per#sample.com">
<input type="hidden" name="item_name" value="registration charge {{ request.session.username }}">
<input type="hidden" name="item_number" value="1">
<input type="hidden" name="amount" value="9.00">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="AU">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="https://www.paypal.com/en_AU/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypal.com/en_AU/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="return" value="http://url/payment_result?response=success">
<input type="hidden" name="cancel_return" value="http://url/sorry">
</form>
</p>
{% endblock %}
Without changing any views, how can I print the value for request.session.username in this template?
In settings.py, TEMPLATE_CONTEXT_PROCESSORS should include django.core.context_processors.request - This makes the request variable available to your templates.
Although, a word of warning: When the user activates his email and signs in after returning, the session variable will probably have changed.