On my Django app, I am trying to make an eBay type of site that lets people post listings with pictures and bid on them. Everything works except posting an image. The image won't show and I do not know if it's the HTML or the python. If you have any questions let me know.
Html code:
{% extends "auctions/layout.html" %}
{% block body %}
<div class="container">
<h2>Create listing</h2>
</div>
<div class="container">
<form action="{% url 'submit' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlInput1">Title</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Title of the lisiting..." name="title" required>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" placeholder="Description..." name="description" required></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Category</label>
<select class="form-control" id="exampleFormControlSelect1" name="category" required>
<option>Fashion</option>
<option>Tools</option>
<option>Toys</option>
<option>Electronics</option>
<option>Home accessories</option>
<option>Books</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Initial Bid</label>
<input type="number" class="form-control" id="exampleFormControlInput1" placeholder="Starting bid..." name="price" required>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Image link (optional)</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="https://blah-blah.jpg..." name="link">
</div>
<button class="btn btn-outline-info" type="submit">Submit</button>
</form>
</div>
{% endblock %}
This is the python function for making listings:
def create(request):
try:
w = Watchlist.objects.filter(user=request.user.username)
wcount=len(w)
except:
wcount=None
return render(request,"auctions/create.html",{
"wcount":wcount
})
Related
I have an html template and I want to print a pdf with weayprint for it. However, I am getting the error "<weasyprint.HTML object at 0x7f6f944df190> 2 extra bytes in post.stringData array problem" that I mentioned in the title. I am sharing my views.py file and some of my html template.
views.py
def export_pdf(id):
print('export')
edit_ambulanceCase = CallCenter.objects.all()
html_string = render_to_string('forms/update_forms/pdf.html',{'PreCaseControl':edit_ambulanceCase})
print(html_string)
html = HTML(string=html_string)
print(html)
return HttpResponse(html.write_pdf('deneme.pdf'))
pdf.html
<main>
<div class="container-fluid px-4">
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="card shadow-lg border-0 rounded-lg mt-5">
<div class="card-header"><h4 class="text-center font-weight-light my-4">Çağrı Merkezi Formu</h4></div>
<div class="card-body">
<form action="" method="POST">
{% csrf_token%}
<h3>Çağrıyı Yapan</h3>
<div class="row">
<div class="mb-3 col">
<label for="institution_name" class="form-label">KURUM ADI</label>
<input type="text" class="form-control" id="institution_name" name="institution_name" value="{{CallCenter.institution_name}}">
</div>
<div class="mb-3 col">
<label for="caller_username" class="form-label">ADI SOYADI</label>
<input type="text" class="form-control" id="caller_username" name="caller_username" value="{{CallCenter.caller_username}}">
</div>
</div>
<div class="row">
<div class="mb-3 col">
<label for="proximity" class="form-label">YAKINLIĞI</label>
<input type="text" class="form-control" id="proximity" name="proximity" value="{{CallCenter.proximity}}">
</div>
<div class="mb-3 col" data-type="control-phone">
<label for="caller_tel_no" class="form-label">TELEFON NUMARASI</label>
<input type="tel" class="form-control" id="caller_tel_no" name="caller_tel_no" value="{{CallCenter.caller_tel_no}}" data-component="phone" inputmode="text" maskvalue="(###) ###-####" val>
<label class="form-sub-label" style="min-height: 13px;" for="caller_tel_no_sub">Lütfen geçerli bir telefon numarası girin.</label>
</div>
</div>
<h3>Hasta</h3>
<div class="row">
<div class="mb-3 col">
<label for="patient_username" class="form-label">ADI SOYADI</label>
<input type="text" class="form-control" id="patient_username" name="patient_username" value="{{CallCenter.patient_username}}">
<label for="patient_age" class="form-label">YAŞ</label>
<input type="text" class="form-control" id="patient_age" name="patient_age" value="{{CallCenter.patient_age}}">
</div>
<div class="mb-3 col">
<label for="" class="form-label">CİNSİYET</label>
<div >
<input class="form-check-input" type="radio" id="male" name="gender" {% if CallCenter.gender == 'erkek' %} checked {%endif%} value="erkek">
<label for="male" class="form-label">Erkek</label>
</div>
<div>
<input class="form-check-input" type="radio" id="female" name="gender" {% if CallCenter.gender == 'kadın' %} checked {%endif%} value="kadın">
<label for="female" class="form-label">Kadın</label>
</div>
</div>
</div>
My html template continues as I have attached. It does not contain any other content. I couldn't include the whole thing because it was too long.
please I would like to ask for help. I'm creating a web-based application and I'm having a hard time when I try to get the data from the HTML form and receive it through the POST method in Django.
It's pulling data only from one field and I really appreciate your help in figuring out why it's not pulling information from the other fields only from the location field. Thank you very much!
HTML = create-request.html
<form action="{% url 'save_request' %}" id="checklistForm" name="checklistForm" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<section id="step-1" class="form-step">
<div class="mt-3">
{% include 'home/1_resource-request.html' %}
</div>
</section>
</form>
HTML = home/1_resource-request.html
{% block stylesheets %}{% endblock stylesheets %} {% block content %}
<div class="pcoded-content">
<div class="pcoded-inner-content">
<div class="main-body">
<div class="page-wrapper">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<h5>Resource Request</h5>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<form>
<div class="form-group">
<label for="checklistID">Checklist Number</label>
<input type="text" class="form-control" id="checklistID" name="checklistnum" placeholder="123456" disabled/>
</div>
<div class="form-group">
<label for="location_ID">CMPA Location</label>
<select class="form-control" id="location_ID" name="location">
<option selected>Select</option>
<option>Brazil</option>
<option>Canada</option>
<option>Mexico</option>
<option>SSA</option>
<option>United States</option>
</select>
</div>
</form>
</div>
<div class="col-md-6">
<form>
<div class="form-group">
<label for="support_id">CMPA Support Needed</label>
<select class="form-control" id="support_id" name="support">
<option selected>Select</option>
<option>Both</option>
<option>PMA - Financial Management</option>
<option>PMO - Project Administration</option>
</select>
</div>
<div class="form-group">
<label for="band_pma_id">Band (PMA)</label>
<select class="form-control" id="band_pma_id" name="band_pma">
<option selected>Select</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<small id="textInfo1" class="form-text text-muted">There is no B4 for US and CA - in progress.</small>
</div>
<div class="form-group">
<label for="band_pmo_id">Band (PMO)</label>
<select class="form-control" id="band_pmo_id" name="band_pmo">
<option selected>Select</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<small id="textInfo2" class="form-text text-muted"
>There is no B4 for US and CA - in progress.</small
>
</div>
<div class="mt-3">
<button class="button btn-navigate-form-step" type="button" step_number="2">Next</button>
<button class="button2 btn btn-outline-primary " type="submit">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock content %}
Django/Python = views.py
def save_request(request):
#print(request.POST)
if request.method == 'POST':
context = {}
location = request.POST.get('location', None)
support = request.POST.get('support', None)
band_PMA = request.POST.get('bandPMA', None)
band_PMO = request.POST.get('bandPMO', None)
ippf = request.POST.get('ippf', None)
print(location, support, band_PMA, band_PMO, ippf)
return render(request, 'home/create-request.html', context=context)
That html defines many different forms. The first form has the location input element, the second form has the support input element, and so on.
Only one form can be submitted at a time. By default it is the first form on the page, so that's why you only see the location element.
I think you need to rewrite the html to have only one form.
That is because you have many tags.
You need to keep only one, the one wrapping your include, so do that :
<form action="{% url 'save_request' %}" id="checklistForm" name="checklistForm" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<section id="step-1" class="form-step">
<div class="mt-3">
{% include 'home/1_resource-request.html' %}
</div>
</section>
</form>
and that :
HTML = home/1_resource-request.html
{% block stylesheets %}{% endblock stylesheets %} {% block content %}
<div class="pcoded-content">
<div class="pcoded-inner-content">
<div class="main-body">
<div class="page-wrapper">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<h5>Resource Request</h5>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="checklistID">Checklist Number</label>
<input type="text" class="form-control" id="checklistID" name="checklistnum" placeholder="123456" disabled/>
</div>
<div class="form-group">
<label for="location_ID">CMPA Location</label>
<select class="form-control" id="location_ID" name="location">
<option selected>Select</option>
<option>Brazil</option>
<option>Canada</option>
<option>Mexico</option>
<option>SSA</option>
<option>United States</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="support_id">CMPA Support Needed</label>
<select class="form-control" id="support_id" name="support">
<option selected>Select</option>
<option>Both</option>
<option>PMA - Financial Management</option>
<option>PMO - Project Administration</option>
</select>
</div>
<div class="form-group">
<label for="band_pma_id">Band (PMA)</label>
<select class="form-control" id="band_pma_id" name="band_pma">
<option selected>Select</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<small id="textInfo1" class="form-text text-muted">There is no B4 for US and CA - in progress.</small>
</div>
<div class="form-group">
<label for="band_pmo_id">Band (PMO)</label>
<select class="form-control" id="band_pmo_id" name="band_pmo">
<option selected>Select</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<small id="textInfo2" class="form-text text-muted"
>There is no B4 for US and CA - in progress.</small
>
</div>
<div class="mt-3">
<button class="button btn-navigate-form-step" type="button" step_number="2">Next</button>
<button class="button2 btn btn-outline-primary " type="submit">Save</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock content %}
Form picture
ID is been selected but I'm not getting the values in the form, please check my code files. See in the picture in URL of the browser, update/id is selected, the problem is values are not fetched in the form.
HTML:
<form id="task-form" name="myForm">
{% csrf_token %}
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-building" placeholder="Building name" name="building" value="{{buildings.building}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-postal" placeholder="Postal Code" name="postalCode" value="{{buildings.postalCode}}">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-town" placeholder="town" name="town" value="{{buildings.town}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-street" placeholder="Street" name="street" value="{{buildings.street}}">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-house" placeholder="House No." name="houseNo" value="{{buildings.houseNo}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-info" placeholder="Additional Information" name="additionalInfo" value="{{buildings.additionalInfo}}">
</div>
</div>
</div>
<div class="text-center mt-3">
<button type="submit" id="btn-task-form" class="btn btn-primary ">UPDATE</button>
</div>
</form>
views.py
def update_Building(request, id):
docId = id;
context = {
'buildings': db.collection('Buildings').document(docId).get()
}
return render(request,"EmployeeAdmin/updateBuilding.html", context)
urls.py
path('update/<str:id>/',views.update_Building,name='update_Building'),
This question already has answers here:
Post values from an HTML form and access them in a Flask view
(2 answers)
Closed 4 years ago.
I have a code like this. This is giving me 400 Bad request error. I found that there is only one form in the page from which the form is submitted.
#auth.route('/admin/project/add',methods = ['POST', 'GET'])
def addproject():
if request.method == 'POST':
projectname = request.form['projectname']
c, conn = connection()
query = "SELECT id from projects WHERE UPPER(project)='{}'".format(projectname)
c.execute(query)
value = c.fetchall
if value>0:
return render_template('addproject.html')
else:
flash("Project already exists. Please goto projects page and confirm. If it is and error, please contact devloper.")
return redirect(url_for('addproject'))
else:
return render_template('addproject.html')
I am also adding my HTML code for the form below. I tried a lot and is not able to figure out why I am keeping on getting that error. I also did small changes in my views.py,still the result is same.
<form action="/admin/project/add" id="myForm" method="POST" name="add">
<div class="form-body">
<div class="row p-t-20">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Project Name</label>
<input type="text" id="projectname" class="form-control" placeholder="Please enter project name" required >
</div>
</div>
<!--/span-->
<div class="col-md-6">
<div class="form-group has-danger">
<label class="control-label">Project Location</label>
<input type="text" id="projectlocation" class="form-control form-control-danger" placeholder="Please enter project location" required >
</div>
</div>
<!--/span-->
</div>
<!--/row-->
<div class="row">
<!--/span-->
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Starting Date</label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" required >
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">End Date</label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" required >
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group has-danger">
<label class="control-label">Leader</label>
<input type="text" id="lead" class="form-control form-control-danger" placeholder="Please enter the leader for the project" required >
</div>
</div>
<div class="col-md-6">
<div class="form-group has-danger">
<label class="control-label">Current Status</label>
<select class="form-control" required>
<option value="">--Select--</option>
<option value="T">T</option>
<option value="D">D</option>
<option value="S">S</option>
</select>
</div>
</div>
</div>
<!--/span-->
<!--/span-->
</div>
<!--/row-->
<h5 class="box-title m-t-40">Project Description (optional)</h5>
<hr>
<div class="col-md-6">
<div class="form-group has-danger">
<TEXTAREA rows="8" cols="138"></TEXTAREA>
</div>
</div>
<!--/span-->
</div>
</div><div class="col-md-6">
<div class="form-actions">
<button type="submit" class="btn btn-success" name="add"> <i class="fa fa-plus" value="Add"></i> Add</button>
<button type="button" class="btn btn-inverse" onclick="viewproject();" name="cancel" value="Cancel">Cancel</button>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li<{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</div></div>
</form>
The answer to your question is in the addproject.html file and the way that sends the parameter.
To solve it check for mistakes in the name of the parameter ('projectname'). See if the name is the same in the addproject.html file.
Another common problem is set incorrectly the 'content-type'.
After you edit your question with the form code, I saw you are using input id instead input name in the form.
The correct way is:
<input type="text" name="projectname" ... >
I hope this helps!
so in my home page I extend from 2 things, based on whether or not the user is signed in or not. Then, if they have TRIED to sign in, and failed, i want the inputs to be red:
{% extends extendVar %} #in this case, extendvar=notLoggedIn.html
{% block signIn %}
{% if failure %}
<div class="form-group has-error">
<input type="email" id="email" name="email" placeholder="Email" class="form-control">
</div>
<div class="form-group has-error">
<input type="password" name="password" placeholder="Password" class="form-control">
</div>
{% endif %}
{% endblock %}
notLoggedIn.html then includes from a header page:
<div class="container" style="">
{% include 'header.html' %}
</div>
And this has a block tag:
<div class="header clearfix">
<nav>
<ul class="nav nav-pills pull-left">
<a class="" href="#">
<img alt="Brand" src="{% static 'images/Logo.png' %}" style="height:auto; max-width:470px; margin-top:-22px; margin-bottom:-30px; padding-right:10px;">
</a>
</ul>
<ul class="nav nav-pills pull-right">
<div id="navbar" class="navbar-collapse collapse">
<form class="navbar-form navbar-right" action="{% url 'signIn' %}" method="POST">
{% csrf_token %}
{% block signIn %}
<div class="form-group">
<input type="email" id="email" name="email" placeholder="Email" class="form-control">
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Password" class="form-control">
</div>
<button class="btn btn-primary btn" href="" role="button" style="">Sign in </button>
{% endblock %}
</form>
</div>
</ul>
</nav>
Project name</h3> -->
my only problem is that because i am including header in NotLoggedIn, and then extending that, it doesnt preserve the block tags, and so the signIn blocks are not working. If instead of doing include header, i merely hard-code it, the blocks work perfectly. Any ideas?
According to the django documentation, when you use "include", blocks in the included template are evaluated first, and then substituted in. So in your template, you can't override a block of the included template.
https://docs.djangoproject.com/es/1.9/ref/templates/builtins/#include
Instead of using a block, what if you pass variables down to the include block, to get it to render how you want? For example:
Your template:
{% include 'header.html' with failure=failure %}
header.html:
<div class="form-group {% if failure %}has-error{% endif %}">
<input type="email" id="email" name="email" placeholder="Email" class="form-control">
</div>
<div class="form-group {% if failure %}has-error{% endif %}">
<input type="password" name="password" placeholder="Password" class="form-control">
</div>