author = models.ForeignKey('auth.User', null=True, blank=False) trouble - django

I have a table where I save data(description, x, y, result and creation date) and until now everything works.
I thought then to add a column with the author for each saved line eg:
DES| X | Y | RESULT |CREATION DATE| AUTHOR |
hi | 3| 1 | 4 | 24/02/2015 | username |
then I added in models.py auth:
from django.db import models
from django.utils import timezone
from simpleapp.oper import add_divide
from django.conf import settings
class ElementiTab(models.Model):
author = models.ForeignKey('auth.User', null=True, blank=False)
des = models.CharField(max_length=30)
x = models.FloatField()
y = models.FloatField()
res = models.FloatField(default=0)
created_date = models.DateTimeField(default=timezone.now)
def save(self, *args, **kwargs):
self.res = add_divide(self.x, self.y)
super(ElementiTab, self).save(*args, **kwargs)
def __str__(self):
return self.des
UPDATE:
forms.py
from django import forms
from .models import ElementiTab
class ElementiTabForm(forms.ModelForm):
class Meta:
model = ElementiTab
fields = ('des', 'x', 'y')
views.py
#login_required
def tabval(request):
# if this is a POST request we need to process the form data
valori = ElementiTab.objects.filter().order_by('-created_date')
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = ElementiTabForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
form.save()
# if a GET (or any other method) we'll create a blank form
else:
form = ElementiTabForm()
return render(request, 'simpleapp/simpleapp.html', {'form': form, 'valori': valori})
#user_passes_test(lambda u: u.is_superuser)
def delete(request, id):
valori_to_delete = get_object_or_404(ElementiTab, pk=id).delete()
return redirect(tabval)
simpleapp.html
{% extends 'registration/base_reg.html' %}
{% block title %}SimpleApp-tabval{% endblock %}
{%block content%}
<h4>TABELLA CON DATI</h4>
<form action="/simpleapp/" method="post">
{% csrf_token %}
{{ form.as_table }}
<input type="submit" value="LIST" />
</form>
<form action="/simpleapp/" method="DELETE">
{% csrf_token %}
<input type="submit" name="canc" value="RESET" />
</form>
<br />
<br />
<div class="table-responsive">
<table class="table table-bordered">
<tr class="info">
<td width="15%" align="center"> NOME</td>
<td width="15%" align="center"> X </td>
<td width="15%" align="center"> Y </td>
<td width="15%" align="center"> RISULTATO </td>
<td width="15%" align="center"> DATA CREAZIONE </td>
<td width="15%" align="center"> AUTORE </td>
{% for elementi in valori %}
<div class="elementi">
<tr>
<td>{{elementi.des}}</td>
<td>{{elementi.x}}</td>
<td>{{elementi.y}}</td>
<td>{{elementi.res}}</td>
<td>{{elementi.created_date}}</td>
<td>{{elementi.author}}</td>
<td width="1%">
{% if user.is_superuser %}
Delete
{% else %}
<span style='font-size: small'>Only Admin</span>
{% endif %}
</td>
</div>
{% endfor %}
</table>
</div>
{% endblock content %}
The fact is that the admin page displays a drop-down menu from which I (as administrator) can choose one of the registered user and so I add them both in the table of my app and in the db.
How can I make this process automatic? I.e. after the login, you put data in the table and once saved the data, also the username is saved and should not be the administrator to set it.
I searched a similar question here but I have not found one to help me to solve my problem.

I updated my answere, i misenderstood your question.
Change this in your view
if form.is_valid():
# Creating the object without commiting to database
obj = form.save(commit=False)
# Setting the user from request
obj.author = request.user
# Commiting to the database
obj.save()

Related

Django Formset Delete Field Not Showing

I need to manually render my formset in my template and I cannot get the delete checkbox field into the template when I render manually. However, it does show when I render {{form.as_table}}.
views.py
QuoteManifestForm= modelformset_factory(QuoteManifest, QManifestForm, can_delete = True)
template - this does not display the {{form.DELETE}} but every other field shows fine, including id which I can see in the DOM.
{{ manifest.management_form }} <--!I passed QuoteManifestForm to template as 'manifest'-->
{% for form in manifest.forms %}
<div id="form_set">
<table id = 'manifest-table25' class="manifest-table2" width=100%>
{% csrf_token %}
<tbody width=100%>
<tr class="manifest-row">
<td width = 17.5% class="productCode" onchange="populateProduct(this)">{{form.ProductCode}}</td>
<td width = 32.5% class="description">{{form.DescriptionOfGoods}}</td>
<td width = 12.5% class="quantity" oninput="calculateUnit(this)">{{form.UnitQty}}</td>
<td width = 12.5% class="unitType">{{form.Type}}</td>
<td width = 12.5% class="price" oninput="calculate(this)">{{form.Price}}</td>
<td width = 12.5% class="amount2">{{form.Amount}}</td>
<td>{{form.DELETE}}</td>
{{form.id}}
</tr>
</tbody>
</table>
</div>
{% endfor %}
Any idea why that is not working?
UPDATE:
I found in the django docs that if you are rendering manually, you should include something like the below in your form. I tried this, but still no Delete field appears in my template:
{% if form2.can_delete %}
<td> {{form.DELETE}}</td>
{% endif %}
CODE TO REPRODUCE
views.py
def QuoteView(request):
QuoteManifestForm= modelformset_factory(QuoteManifest, fields =('ProductCode', 'DescriptionOfGoods', 'UnitQty', 'Type','Amount', 'Price'), can_delete = True)
if request.method == "POST":
form2 = QuoteManifestForm(request.POST)
form2.save()
return redirect('HomeView')
else:
form2 = QuoteManifestForm()
context = {
'form2': form2,
}
return render(request, 'quote.html', context)
quote.html
{{ form2.management_form }}
<div id="form_set">
{% for form2 in form2.forms %}
<table id = 'manifest-table25' class="manifest-table2" width=100%>
{% csrf_token %}
<tbody width=100%>
<tr class="manifest-row">
<td width = 17.5% class="productCode" onchange="populateProduct(this)">{{form2.ProductCode}}</td>
<td width = 32.5% class="description">{{form2.DescriptionOfGoods}}</td>
<td width = 12.5% class="quantity" oninput="calculateUnit(this)">{{form2.UnitQty}}</td>
<td width = 12.5% class="unitType">{{form2.Type}}</td>
<td width = 10.5% class="price" oninput="calculate(this)">{{form2.Price}}</td>
<td width = 12.5% class="amount2">{{form2.Amount}}</td>
<td>{{form2.DELETE}}</td>
{{form2.id}}
</tr>
</tbody>
</table>
{% endfor %}
</div>
models.py
class QuoteManifest(models.Model):
ProductCode = models.ForeignKey(Product, null=True, blank=True)
DescriptionOfGoods = models.CharField(max_length=500, blank=True)
UnitQty = models.CharField(max_length=10, blank=True)
Type = models.CharField(max_length=50, blank=True)
Amount = models.CharField(max_length=100, blank=True)
Price = models.CharField(max_length=100, blank=True)
{{form2.DELETE}} is supposed to render a checkbox. This is what I cannot get working. It does work when I render the form as {{form2.as_p}} but that will not work for me in my case.
Using your example code, I was able to create a sample that seems to render the checkbox using the {{ form.DELETE }} syntax.
It appears that my sample code is very similar to what you already have. I did add an on_delete parameter to the ProductCode variable in the QuoteManifest model. And I'm not sure what your Product model looks like so I just created a dummy model. I also removed your CSS classes and JavaScript calls. Is it possible that something in your JavaScript was overriding the checkboxes?
As you'll see in my sample, I do get the checkbox. My code is below, and here is a link to the working demo on repl.it.
models.py
from django.db import models
class Product(models.Model):
ProductName = models.CharField(max_length=100, unique=True)
class QuoteManifest(models.Model):
ProductCode = models.ForeignKey(Product, null=True, blank=True, on_delete=models.CASCADE)
DescriptionOfGoods = models.CharField(max_length=500, blank=True)
UnitQty = models.CharField(max_length=10, blank=True)
Type = models.CharField(max_length=50, blank=True)
Amount = models.CharField(max_length=100, blank=True)
Price = models.CharField(max_length=100, blank=True)
views.py
from django.shortcuts import render, redirect
from django.forms.models import modelformset_factory
from .models import QuoteManifest
def QuoteView(request):
QuoteManifestForm= modelformset_factory(QuoteManifest, fields =('ProductCode', 'DescriptionOfGoods', 'UnitQty', 'Type','Amount', 'Price'), can_delete=True)
form2 = QuoteManifestForm()
context = {
'form2': form2,
}
return render(request, 'quote.html', context)
templates/quote.html
<div id="form_set">
{% for form2 in form2.forms %}
<table id="manifest-table25" width=100%>
{% csrf_token %}
<tbody width=100%>
<tr>
<td>{{form2.ProductCode}}</td>
<td>{{form2.DescriptionOfGoods}}</td>
<td>{{form2.UnitQty}}</td>
<td>{{form2.Type}}</td>
<td>{{form2.Price}}</td>
<td>{{form2.Amount}}</td>
<td>{{form2.DELETE}}</td>
</tr>
</tbody>
</table>
{% endfor %}
</div>
Based on the examples here: https://docs.djangoproject.com/en/3.0/topics/forms/formsets/#manually-rendered-can-delete-and-can-order
It looks like your view would be:
def QuoteView(request):
QuoteManifestFormset= modelformset_factory(QuoteManifest, fields =('ProductCode', 'DescriptionOfGoods', 'UnitQty', 'Type','Amount', 'Price'), can_delete = True) # Renamed as formset for clarity
if request.method == "POST":
formset = QuoteManifestFormset(request.POST) # also renamed
formset.save()
return redirect('HomeView')
else:
formset = QuoteManifestFormset()
context = {'formset': formset}
return render(request, 'quote.html', context)
And your quote.html I think {% for form2 in form2.forms %} needs to be {% for form in formset %}
<form method="post">
{{ formset.management_form }}
{% for form in formset %}
<table id = 'manifest-table25' class="manifest-table2" width=100%>
{% csrf_token %}
<tbody width=100%>
<tr class="manifest-row">
<td width = 17.5% class="productCode" onchange="populateProduct(this)">{{form.ProductCode}}</td>
<td width = 32.5% class="description">{{form.DescriptionOfGoods}}</td>
<td width = 12.5% class="quantity" oninput="calculateUnit(this)">{{form.UnitQty}}</td>
<td width = 12.5% class="unitType">{{form.Type}}</td>
<td width = 10.5% class="price" oninput="calculate(this)">{{form.Price}}</td>
<td width = 12.5% class="amount2">{{form.Amount}}</td>
<td>{{form.DELETE}}</td>
{{form.id}}
</tr>
</tbody>
</table>
{% endfor %}
</form>

why is input from my form not writting to the sqlite3 database in django

I would like to update my list after adding some inputs through a form but i cannot see my updated list. I see the existing items in my list ,but when i add a new item it does not appear on the list. I can manually add it using the admin pannel and view it in the list(a whole different path),but not with the form i created to take input and update the list. I was able to query my database and input from the form is not getting written to the database, that's why its not displaying any changes.Below is my code
models.py
class BlogPost(models.Model):
notes = models.CharField(max_length = 1000000000000000000000000000)
date = models.DateTimeField(auto_now_add=True)
done = models.BooleanField(default=False)
def __str__(self):
return self.notes
form.py
from blog.models import BlogPost
class BlogForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['notes', 'done',]
views.py
from django.shortcuts import render,redirect
from django.http import HttpResponse,HttpResponseRedirect,HttpRequest
from blog.models import BlogPost
from blog.form import BlogForm
def home(request):
context = {
'welcome_text': 'Welcome to the home page. View some more stuff soon'
}
return render(request,'home.html', context)
def blogpost(request):
if request.method == "POST":
form = BlogForm(request.POST)
if form.is_valid():
if form.save():
message.success(request, "the task was added")
return redirect('blogpost')
else:
all_blogs = BlogPost.objects.all
return render(request, 'blog.html',{'the_blogs': all_blogs } )
blog.html
{%extends 'base.html' %}
{% block title%}
<title> Blog </title>
{% endblock title%}
{%block content %}
<div class="container">
<br>
{%for message in messages%}
{{message}}
{% endfor %}
<form method = 'POST'>
{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" name = 'blog' placeholder = 'new blog' >
</div>
<button type="submit" class="btn btn-primary">Add Blog</button>
</form>
<br>
<table class="table table-hover table-dark">
<thead>
<tr>
<th scope="col">Blog </th>
<th scope="col">Done</th>
<th scope="col">Date</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{% for item in the_blogs %}
{% if item.done %}
<tr class="table-success">
<td >{{item.notes}}</td>
<td >Not-Completed</td>
<td>{{item.date}}</td>
<td>edit</td>
<td>delete</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
{%endblock content%}
if you need more information regarding this, here is a link to my GitHub repository with more code.
https://github.com/wfidelis/Django-App
You have to correct the code indentation and the get call part, pass the form to context object and call it with double curly brackets on templates, also add an action attribute to the template.
def blogpost(request):
all_blogs = BlogPost.objects.all()
if request.method == "POST":
form = BlogForm(request.POST)
if form.is_valid():
if form.save():
message.success(request, "the task was added")
return redirect('blogpost')
else:
form = BlogForm()
return render(request, 'blog.html',{'form': form, 'the_blogs': all_blogs } )
<form method='POST' action="">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Add Blog</button>
<form/>
When you add a blog, don't redirect, try rendering the page with the new list the same as how you did it here:
all_blogs = BlogPost.objects.all
return render(request, 'blog.html',{'the_blogs': all_blogs } )
or try returning new object created as JSON format to the front-end (as a response of the POST request) and front-end will add it to the HTML with jQuery or JS

Having an edit form and a detail view on the same page

I am currently trying to get an edit form working on the same page as a detail view in Django.
I am currently trying out the way as recommended on the docs (i.e. using FormMixin). So, my view looks like this:
class ProductiveMinutesDetail(FormMixin, DetailView):
model = models.ProductiveMinutes
pk_url_kwarg = 'productiveminutes_pk'
form_class = forms.EditForm
def get_success_url(self):
return reverse_lazy('productiveminutes_list')
def get_context_data(self, **kwargs):
context = super(ProductiveMinutesDetail, self).get_context_data(**kwargs)
context['form'] = forms.EditForm(initial={'post': self.object})
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
return super(ProductiveMinutesDetail, self).form_valid(form)
And my form looks like this:
from django import forms
from . import models
class EditForm(forms.ModelForm):
class Meta:
model = models.ProductiveMinutes
fields = ('name', 'description',)
The model I am using is this:
class Scenario(models.Model):
name = models.CharField(max_length=200)
class ProductiveMinutes(models.Model):
scenario = models.OneToOneField(Scenario)
name = models.CharField(max_length=200)
amount = models.DecimalField(max_digits=50, decimal_places=2)
description = models.CharField(max_length=200)
Using this I can get the form to render on the page but I know I am doing something wrong as the fields are empty when I would like them to be populated with the data that is already present.
Another piece of complexity is that this form should not be editing the amount field of the model just the name and description. The amount value is edited separately from the detail view of this page.
So, I guess my main question is how can I get the form to be populated with the data for the models fields that is already present and then edit it. Ideally functionality like that of the generic UpdateView that Django provides.
I am using Django version 1.10
Any help with this would be much appreciated
Thanks for your time
UPDATE:
My template looks like this:
{% extends 'pages/dashboard.html' %}
{% load i18n humanize crispy_forms_tags %}
{% block content %}
<div>
<h1 class="text-center">Productive Minutes: {{ productiveminutes.name }}</h1>
<div class="row">
<div class="col"></div>
<div class="col-md-8 col-lg-8">
<h3>Edit productive minutes: {{ productiveminutes.name }}</h3>
<form role="form" method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="primaryAction btn btn-primary pull-right" type="submit">{% trans "Submit" %}</button>
</form>
</div>
<div class="col"></div>
</div>
<hr>
<div class="row">
<div class="col"></div>
<div class="col-md-8 col-lg-8">
<h3>Data Records</h3>
<div class="table-responsive">
<table class="table table-bordered">
<thead class="thead-default">
<tr>
<th>ID</th>
<th>Productive Minutes</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ productiveminutes.id }}</td>
<td>{{ productiveminutes.amount|intcomma }}</td>
<td>
<i class="fa fa-pencil"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col"></div>
</div>
</div>
{% endblock content %}
The example in the docs is for displaying detail for an object and having a separate contact form.
In your case, it sound like you want to display the ProductiveMinutesDetail object, and have a form that allows you to update some of the fields of that same object. In that case, you should just use UpdateView.
class ProductiveMinutesUpdate(UpdateView):
model = models.ProductiveMinutes
pk_url_kwarg = 'productiveminutes_pk'
form_class = forms.EditForm
success_url = reverse('productiveminutes_list')
def get_context_data(self, **kwargs):
context = super(ProductiveMinutesUpdate, self).get_context_data(**kwargs)
# Refresh the object from the database in case the form validation changed it
object = self.get_object()
context['object'] = context['productiveminutes'] = object
return context

How to upload a file from webpage and show the name in table in Django

I have been doing the research for a long time and cannot figure out. I have my Django app linked to mysql db which was created by my model "CostDetl", and I have a webpage to display my model table. In the table, I have a filefield "Agreement", where I would like the webpage to show browse and upload form in the cell for each table row, and if anything is uploaded and the filefield is not empty, the table cell would just show the file name with a link to open the uploaded file. I have no problem displaying the uploaded file with the link and I can upload the agreement in Admin, but I cannot figure out how to upload from the webpage. Please help! The following are all my code, much appreciated!
Settings:
MEDIA_ROOT = '/Users/username/myapp/media'
MEDIA_URL = '/media/'
In project urls.py, I added:
url(r'^media/', views.UploadAgreementView),
In models.py:
def content_file_name(instance, filename):
return os.path.join(['content', instance.user.username, filename])
class CostDetl(models.Model):
Provider = models.CharField(null=True, blank=True, max_length=200)
Description = models.CharField(null=True, blank=True, max_length=200)
Agreement = models.FileField(null=True, blank=True, upload_to=content_file_name)
def __unicode__(self):
return self.Provider+' | '+self.Description
def __str__(self):
return self.Provider+' | '+self.Description
Views:
def CostData(request):
costrecordset = CostDetl.objects.all()
context = {'CostDetl1': costrecordset}
return render(request, 'polls/CostDetl.html', context)
def UploadAgreementView(request):
if request.method == 'POST':
form = UploadAgreement(request.POST or None, request.FILES or None)
if form.is_valid():
newdoc = CostDetl(request.FILES['file'])
newdoc.save()
return HttpResponseRedirect(reverse('polls:AgreementUpload'))
else:
form = UploadAgreement()
documents = CostDetl.objects.all()
return render(request,
'polls/CostDetl.html',
{'CostDetl1': documents, 'AgreementUpload': form})
In forms.py:
from django import forms
from .models import CostDetl
class UploadAgreement(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
In template:
{% if CostDetl1 %}
{% load humanize %}
<table>
<tr>
<th>Provider</th>
<th>Expense Description</th>
<th>Agreement</th>
</tr>
{% for i in CostDetl1 %}
<tr>
<td nowrap>{{ i.Provider }}</td>
<td nowrap>{{ i.Description }}</td>
<td nowrap>
{% if i.Agreement %}
<a href="/media/{{ i.Agreement }}">
{{ i.Agreement }}
</a>
{% else %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="file" name="myfile"/>
<input type="submit" name="submit" value="Upload"/>
</form>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
{% endif %}
In urls.py:
url(r'^CostDetl/$', views.CostData, name='CostDetl1'),
url(r'^Agreement/$', views.UploadAgreementView, name='AgreementUpload'),

My django python functions not running correctly

I am currently working on an e-commerce web app and decided to use the Beginning Django Ecommerce book. I am following the content and implementing it in my own way but i am having some issues with some few functions that are not running.
here are the apps with the files where i think the problem is coming from;
1. cart app models.py:
from django.db import models
from menu_items.models import Item
from smartmin.models import SmartModel
import django.db.models.options as options
options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('augment_quatity','name','price','get_absolute_url','total',)
class OrderItem(SmartModel):
order_id = models.CharField(max_length=50)
date_added = models.DateTimeField(auto_now_add=True)
quantity = models.IntegerField(default=0)
item = models.ManyToManyField(Item)
class Meta:
db_table='order_items'
def __unicode__(self):
return "%s" % (self.order_id)
def total(self):
return self.quatity *self.item.price
def name(self):
return self.item.name
def price(self):
return self.item.price
def get_absolute_url(self):
return self.item.get_absolute_url()
# incase user orders same item twice we jus add on the quantity
def augment_quatity(self, quantity):
self.quatity = self.quantity + int(quantity)
self.save
orders.py in the same app:
from cart.models import OrderItem
#from cart.models import order_id
from menu_items.models import Item
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
import decimal
import random
ORDER_ID_SESSION_KEY = 'order_id'
# get the current user's cart id, sets new one if blank
def _order_id(request):
if request.session.get(ORDER_ID_SESSION_KEY,'') == '':
request.session[ORDER_ID_SESSION_KEY] = _generate_cart_id
return request.session[ORDER_ID_SESSION_KEY]
def _generate_cart_id():
order_id =''
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()'
order_id_length = 100
for y in range(order_id_length):
order_id += characters[random.randint(0,len(characters)-1
)]
return order_id
# return all items from the current user's order
def get_order_items(request):
return OrderItem.objects.filter(order_id=_order_id(request))
# add an item to order
def add_to_order(request):
postdata = request.POST.copy()
#get item slug from post data, return blank if empty
# item_slug = postdata.get('item_slug','')
#get quantity added, return 1 if empty
quantity = postdata.get('quantity',1)
# fetch the item or return missing page error_message
i = get_object_or_404(Item,)
# get items in order
order_items = get_order_items(request)
item_in_orders = False
# check to see if item is already in cart
for order_item in order_items:
if order_item.item.id == i.id:
#update the quantity if found
order_item.augment_quantity(quantity)
item_in_order = True
if not item_in_order:
# creat and save a new order item
oi = OrderItem()
oi.item = i
oi.quantity = quantity
oi.order_id = _order_id(request)
oi.save()
2.live app views.py
def show_order(request):
if request.method == 'POST':
postdata = request.POST.copy()
if postdata['submit'] == 'Remove':
order.remove_from_order(request)
if postdata['submit'] == 'Update':
order.update_order(request)
order_items = order.get_order_items(request)
page_title = 'F4L order'
order_subtotal = order.order_subtotal(request)
return render_to_response('public/order.html',context_instance=RequestContext(request))
Template where the functionality is not working,
{% extends "base.html" %}
{% block content %}
{% load menu_tags %}
<div style="height:30px">
{% order_box request %}
</div>
<table summary="Your menu order" id="menu_order">
<caption>Your F4L Orders</caption>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Price</th>
<th scope="col" class="right">Total</th>
</tr>
</thead>
<tfoot>
<tr>
<th class="right" colspan="2">
Order Subtotal:
</th>
<th class="right">
{{order_subtotal}}<span> frw</span>
</th>
</tr>
{% if order_items %}
<tr>
<th class="right" colspan="2">
Checkout Now
</th>
</tr>
{% endif %}
</tfoot>
<tbody>
{% if order_items %}
{% for item in order_items %}
<tr>
<td>
{{ item.name }}
</td>
<td>{{ item.price }}<span> frw</span></td>
<td class="right">
<form method="post" action="." class="order">
<label for="quantity">Quantity:</label>
<input type="text" name="quantity" value="{{ item.quantity }}" id="quantity" size="2" class="quantity" max_length="5" />
<input type="hidden" name="item_id" value="{{ item.id }}" />
</td>
<td>
<input type="submit" name="submit" value="update"/>
</form>
</td>
<td>
<form method="post" action="." class="order">
<input type="hidden" name="item_id" value="{{ item.id }}" />
</form>
</td>
<td>
<form method="post" action="." class="order">
<input type="hidden" name="item_id" value="{{ item.id}}" />
<input type="submit" name="submit" value="Remove" />
</form>
</td>
<td class="right">{{ item.total }}<span> frw</span></td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="2" style="height:30px;">
Your F4L order is empty.
</td>
</tr>
{% endif %}
</tbody>
</table>
{% endblock %}
Now th problem is, the above template code is a page a user redirects to after submitting a form with the quantity of item he/she is buying but that is not happening. After submitting form with for example 10items,i redirect to this page(above _template_), it loads correctly but does not return the information i submitted.
i do understand that this is alot but i have really need your help and will appreciate any sort of help.
In show_order view you should pass your variables to template as dictionary:
...
context_dict = {'order_items': order_items, 'order_subtotal': order_subtotal}
return render_to_response('public/order.html', context_dict, context_instance=RequestContext(request))