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 %}
Related
I wanna create a appointment system but its not working now. I'dont have a error actually i just have a error and it's coming from views.py (messages.warning(request,"ERROR!")). When i opened the page its coming anyway i just wanna send my informations.
models.py: I created models but im not sure for policlinic and doctor because it doesn't seems like charfield text. It's a option field in HTML.
class Randevu(models.Model):
STATUS = (
('New', 'Yeni'),
('True', 'Evet'),
('False', 'Hayır'),
)
policlinic=models.ForeignKey(Policlinic,on_delete=models.CASCADE)
user=models.ForeignKey(User,on_delete=models.CASCADE)
doctor=models.ForeignKey(Doctors,on_delete=models.CASCADE)
phone = models.CharField(max_length=15)
email = models.CharField(max_length=50)
date=models.DateField(null=True)
time=models.TimeField(null=True)
payment= models.CharField(max_length=50)
insurance= models.CharField(max_length=50)
note=models.TextField(max_length=200)
status = models.CharField(max_length=10, choices=STATUS,default='New')
ip=models.CharField(max_length=20,blank=True)
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
def str(self):
return self.user.username
class AppointmentForm(ModelForm):
class Meta:
model=Randevu
fields=['phone','email','date','time','payment','insurance','note']
views.py:
def randevu(request):
setting=Setting.objects.get(pk=1)
policlinic=Policlinic.objects.all()
doctor=Doctors.objects.all()
context={'setting':setting ,'doctor':doctor,'policlinic':policlinic}
if request.method=='POST':
form=AppointmentForm(request.POST)
if form.is_valid():
current_user=request.user
data=Randevu()
data.user_id=current_user.id
data.phone=form.cleaned_data['phone']
data.email=form.cleaned_data['email']
data.date=form.cleaned_data['date']
data.time=form.cleaned_data['time']
data.payment=form.cleaned_data['payment']
data.insurance=form.cleaned_data['insurance']
data.note=form.cleaned_data['note']
data.ip=request.META.get('REMOTE_ADDR')
data.save()
messages.success(request,"DONE! :)")
return render(request,'randevu.html',context)
messages.warning(request,"ERROR!")
return render(request,'randevu.html',context)
urls.py
urlpatterns = [
path('randevu',views.randevu,name='randevu') ]
randevu.html
<!-- MAKE AN APPOINTMENT -->
<section id="appointment" data-stellar-background-ratio="3">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6">
<img src="{% static 'images/appointment-image.jpg' %}" class="img-responsive" alt="">
</div>
<div class="col-md-6 col-sm-6">
<!-- CONTACT FORM HERE -->
<form id="appointment-form" role="form" action="{% url 'randevu' %}" method="post">
<!-- SECTION TITLE -->
<div class="section-title wow fadeInUp" data-wow-delay="0.4s">
<h2>Randevu Alın</h2>
{%if messages%}
{%for message in messages%}
<div class="alert alert-{{message.tags}}"role="alert">
{{message}}
</div>
{%endfor%}
{%endif%}
</div>
{% csrf_token %}
<div class="wow fadeInUp" data-wow-delay="0.8s">
<div class="col-md-6 col-sm-6">
<label for="email">E-mail</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Your Email">
</div>
<div class="col-md-6 col-sm-6">
<label for="telephone">Telefon Numaranız</label>
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone">
</div>
<div class="col-md-6 col-sm-6">
<label for="date">Tarih</label>
<input type="date" name="date" id="date" value="" class="form-control">
</div>
<div class="col-md-6 col-sm-6">
<label for="time">Zaman</label>
<input type="time" name="time" id="time" value="" class="form-control">
</div>
<div class="col-md-6 col-sm-6">
<label for="select">Poliklinik Seçiniz</label>
<select name="policlinic" class="form-control" id="policlinic">
{% for rs in policlinic %}
<option>{{rs.title}}</option>
{%endfor%}
</select>
</div>
<div class="col-md-6 col-sm-6">
<label for="select">Doktor Seçiniz</label>
<select name="doctor" class="form-control" id="doctor">
{% for rs in doctor %}
<option>{{rs.name}}</option>
{%endfor%}
</select>
</div>
<div class="col-md-6 col-sm-6">
<label for="select">Ödeme Yöntemi</label>
<select name="payment" class="form-control" id="payment">
<option>Nakit</option>
<option>Banka Kartı</option>
<option>Kredi Kartı</option>
<option>Diğer</option>
</select>
</div>
<div class="col-md-6 col-sm-6">
<label for="select">Sigorta</label>
<select name="insurance" class="form-control" id="insurance">
<option>SGK</option>
<option>AXA Sağlık Sigortası</option>
<option>Anadolu Sağlık Sigortası</option>
<option>Allianz Sağlık Sigortası</option>
<option>Akbank Sağlık Sigortası</option>
<option>Tamamlayıcı Türkiye Sağlık Sigortası</option>
<option>Diğer (Belirtiniz)</option>
</select>
</div>
<div class="col-md-12 col-sm-12">
<label for="Message">Belirtmek istediğiniz herhangi bir durum</label>
<textarea class="form-control" rows="5" id="note" name="note" placeholder="Notunuz"></textarea>
{%if user.id is not None%}
<button type="submit" class="form-control" id="cf-submit" name="submit">Submit Button</button>
{%else%}
Randevu oluşturmak için login olunuz!
{% endif %}
</div>
</div>
</form>
</div>
</div>
</div>
</section>
It's my appointment page:
enter image description here
Because get method is used to render the page
and in views.py you dont write
if request.method=='GET':
As a result, the error message is executed while there is no error
add this :
if request.method=='GET':
I have two models one is Sales Invoice and Product model but I have only one form to sumbit data
product supposed to be multiple data.
sales_invoice is invoice detail models
Both models field submitted through only from one html form page
I wish to save product data in products and invoice data in invoice with created invoice number.
How Can save data separately in two models ?
views.py
if request.method == 'POST':
#SALES INVOICE
# sales_invoice details
invoice_number=request.POST['invoice_number']
invoice_date=request.POST['invoice_date']
customer_id=request.POST['customer_id']
special_instructions=request.POST['special_instructions']
#invoice total
total=request.POST['total']
CGST=request.POST['CGST']
SGST=request.POST['SGST']
discount=request.POST['discount']
round_off=request.POST['round_off']
grand_total=request.POST['grand_total']
#other details
user=request.POST['user']
remarks=request.POST['remarks']
#ITEM DETAILS
# item_details
sales_invoice_id = request.POST['sales_invoice_id']
items = request.POST['items']
items_obj= New_Stock_Entry.objects.get(item=items)
hsn = request.POST['hsn']
quantity = request.POST['quantity']
unit = request.POST['unit']
unit_obj = Unit_Setting.objects.get(unit_name=unit)
unit_price = request.POST['unit_price']
tax_code = request.POST['tax_code']
amount = request.POST['amount']
sales_details=Sales_Invoice.objects.create(invoice_number=invoice_number,invoice_date=invoice_date,customer_id=customer_id,special_instructions=special_instructions,total=total,CGST=CGST,SGST=SGST,discount=discount,round_off=round_off,grand_total=grand_total,user=user,remarks=remarks)
sales_details.save()
items=Items_Details.objects.bulk_create(sales_invoice_id=sales_invoice_id,items=items_obj,hsn=hsn,quantity=quantity,unit=unit_obj,unit_price=unit_price,tax_code=tax_code,amount=amount)
items.save()
sales_invoice.html
<form method="post">
{% csrf_token %}
<!-- Estimate Details -->
<div class="row pb-3 pt-3">
<div class="col-md mb-2">
<label>Invoice Number</label>
<input type="text" class="form-control" placeholder={{invoice_number}} disabled>
</div>
<div class="col-md mb-2">
<label>Date</label>
<input type="date" class="form-control" placeholder="Invoive_Date">
</div>
<div class="col-md mb-2">
<label>Select Customer</label>
<select id="customer_id" class="form-control" name="Customer">
<option value="">Select Customer</option>
{% if all_customers %}
{% for c in all_customers %}
<option value="{{c.customer_id}}">{{c.first_name}} {{c.surname}}</option>
{% endfor %}
{% endif %}
</select>
</div>
</div>
<div class="row pb-3">
<div class="col-md-4 mb-2">
<label>Contact Person Name</label>
<input type="text" class="form-control" id="contact_person_name" name="contact_person_name"
placeholder="Contact Person">
</div>
<div class="col-md-4 mb-2">
<label>Phone</label>
<input type="number" class="form-control" placeholder="Phone" id="phone" name="phone">
</div>
</div>
<div class="row pb-3">
<div class="col-md mb-2">
<label>Bill To</label>
<textarea type="text" class="form-control" placeholder="Bill To" id="bill_to"
name="bill_to"></textarea>
</div>
<div class="col-md mb-2">
<label>Ship To</label>
<textarea type="text" class="form-control" placeholder="Ship To" id="ship_to"
id="ship_to"></textarea>
</div>
<div class="col-md mb-2">
<label>Special Instructions</label>
<textarea type="text" class="form-control" placeholder="Special Instructions"
id="special_instructions" name="special_instructions"></textarea>
</div>
</div>
<!-- Items -->
<div class="card-header">
<div class="row">
<div class="col-auto m-1 pl-2">
<img src="{% static 'images/GIF/Add.gif'%}" width="30px" height="30px">
</div>
<div class="col mt-2">
<h5>Item Details</h5>
</div>
</div>
</div>
<div>
<!-- <form> -->
<div class="row pb-3 pt-3">
<div class="col-md mb-2">
<label>Item</label>
<select class="form-control" placeholder="Items" id="item" name="item">
<option value="">Item</option>
{% if all_items %}
{% for i in all_items %}
<option value="{{i.item}}">{{i.item}}</option>
{% endfor %}
{% endif %}
</select>
</div>
<div class="col-md mb-2">
<label>HSN</label>
<input type="text" class="form-control" placeholder="HSN" id="hsn" name="hsn">
</div>
<div class="col-md mb-2">
<label>Quantity</label>
<input type="number" class="form-control" placeholder="Quantity" id="quantity"
name="quantity">
</div>
<div class="col-md mb-2">
<label>Unit</label>
<select class="form-control" placeholder="Units" id="unit_name" name="unit_name">
<option value="">Unit</option>
{% if all_unit %}
{% for u in all_unit %}
<option value="{{u.unit_name}}">{{u.unit_name}}</option>
{% endfor %}
{% endif %}
</select>
</div>
<div class="col-md mb-2">
<label>Unit Price</label>
<input type="number" class="form-control" placeholder="Unit Price" id="unit_price"
name="unit_price">
</div>
<div class="col-md mb-2">
<label>Tax</label>
<input type="text" class="form-control" placeholder="Tax" id="tax_code" name="tax_code">
</div>
<div class="col-md mb-2">
<label>Amount</label>
<input type="text" class="form-control" placeholder="Amount" id="amount" name="amount">
</div>
<div class="col-auto">
<P><button type="button" class="w3-btn w3-blue" id="submit">Save</button>
</div>
</div>
<!-- </form> -->
</div>
<br>
<!-- Items table -->
<div id="tab">
<div class="table-responsive-md">
<table class="table table-sm" style="background-color: #e4f7f5;" id="demo">
<tbody id="items_tbody"></tbody>
</table>
</div>
</div>
<!-- Other Details -->
<div class="card-header">
<div class="row">
<div class="col-auto m-1 pl-2">
<img src="../static/images/GIF/Setting.gif" width="30px" height="30px">
</div>
<div class="col mt-2">
<h5>Other Details</h5>
</div>
</div>
</div>
<div class="row pb-3 pt-3">
<div class="col-md mb-2">
<label>User</label>
<input type="text" class="form-control" placeholder={{username}} id="user" name="user">
</div>
<div class="col-md mb-2">
<label>Remarks</label>
<textarea type="text" class="form-control" placeholder="Remarks"></textarea>
</div>
</div>
<div class="row">
<div class="col">
<button class="btn btn-primary" type="add">Save</button>
</div>
</div>
</form>
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
})
Whenever i tried to upload the image through the form it gives me an error which is multivalue dictionary key error. GIVEN BELOW.....
and this image will show on the other side of the project let's say user side. Any hints ..___i also added the full code of my html page....
MultiValueDictKeyError at /hosteladmin/messfood
'photo'
Request Method: POST
Request URL: http://127.0.0.1:8000/hosteladmin/messfood
Django Version: 2.2.4
Exception Type: MultiValueDictKeyError
Exception Value:
'photo'
Exception Location: C:\Users\lenovo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\datastructures.py in __getitem__, line 80
Python Executable: C:\Users\lenovo\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.4
Python Path:
['D:\\django project\\hostel_final',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32',
'C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\win32',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\win32\\lib',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\Pythonwin']
Server time: Mon, 11 Nov 2019 10:07:52 +0000
Please Help me out on this,i shall be very thankfull
VIEWS FILE CODE:-
def messfood(request):
if request.method=="POST":
dn=request.POST['name']
dp=request.POST['price']
ft=request.POST['dfgtime']
fd=request.POST['sdday']
s=Fooddetials()
s.dimage = request.FILES["photo"]
s.dname=dn
s.dprice=dp
s.food_timing=ft
s.food_day=fd
s.save()
return render(request,'hosteladmin/messfood.html')
else:
return render(request,'hosteladmin/messfood.html')
HTML FORM CODE:-
{% extends 'hosteladmin/base.html' %}
{% block content %}
<div class="card">
<div class="card-header card-header-primary">
<h4 class="card-title">Add Mess Food Service</h4>
</div>
<div class="card-body">
<form action="{% url 'messfood' %}" method="post">
{% csrf_token %}
<div class="row">
<div class="col-md-12">
<label class="bmd-label-floating">Dish Image</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="file" name="dimage">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">Dish Name</label>
<input type="text" class="form-control" name="name" required="">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">Dish Price</label>
<input type="text" class="form-control" name="price" required="">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">Food Time</label><br>
<select class="form-control " name="dfgtime" data-style="btn btn-link" id="exampleFormControlSelect1">
<option value="1">BreakFast</option>
<option value="1">Lunch</option>
<option value="1">Dinner</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">Day</label><br>
<select class="form-control " name="sdday" data-style="btn btn-link" id="exampleFormControlSelect1">
<option value="1">Sunday</option>
<option value="1">Monday</option>
<option value="1">Tuesday</option>
<option value="1">Wednesday</option>
<option value="1">Thrusday</option>
<option value="1">Friday</option>
<option value="1">Saturday</option>
</select>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary pull-right">Add Detials</button>
<div class="clearfix"></div>
</form>
</div>
</div>
{% endblock %}
MODELS FILE CODE:-
class Fooddetials(models.Model):
dimage=models.ImageField(upload_to='fooddetails/')
dname=models.CharField(max_length=200)
dprice=models.CharField(max_length=200)
food_timing=models.CharField(max_length=200)
food_day=models.CharField(max_length=200,default="")
ROOT DIRECTORY IN SETTINGS OF THE PROJECT:-
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
put it in a try block and see
try:
s.dimage = request.FILES or None['photo']
except TypeError:
s.dimage = None
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>