views:

71

answers:

3
+1  Q: 

Django question

Hello, I have a django app that suppose to display invoices and clients. Now for some reasons When I run the Django sever, for some reasons it only displays the invoice data in invoice_list but cannot displays the clients data in clients_list. The clients data does shows up in another view, but not in invoice_details view.

EDIT: I Seem to have worked out something. The clients data in clients_list does appear but in the wrong url for some reason. I try to explain it as simple as I can.

urls

(r'^index/invoice_details/1 A client detail appears at this page. (r'^index/invoice_details/2 A client detail appears at this page. (r'^index/invoice_details/3 An invoice shows on this page.

Now I want r'^index/invoice_details/3 to also show what is written in (r'^index/invoice_details/1 but because both numbers are different It will fail.

Here are some outputs that may be helpful.

#models.py
Class Client(models.Model):
    company = models.CharField(max_length=80)
    first_name = models.CharField(max_length=80, blank=True, null=True)
    last_name = models.CharField(max_length=80, blank=True, null=True)
    address = models.CharField(max_length=250)
    city = models.CharField(max_length=100)
    country = models.CharField(max_length=120)
    postcode = models.CharField(max_length=7)
    telephone = models.CharField(max_length=20)
    email = models.EmailField()
    additional_info = models.TextField(blank=True, null=True)

    def __unicode__(self):
            return self.company 

class Invoice(models.Model):
    client = models.ForeignKey(Client)
    date = models.DateField()
    invoice_no = models.CharField(max_length=16)
    work_orders = models.ManyToManyField(Work_Order)
    contract_info = models.ForeignKey(Contract_Info)

    def __unicode__(self):
                return self.invoice_no


    # urls.py
    urlpatterns = patterns('',
       (r'^index/invoice/$', invoice_info),
       (r'^index/invoice_details/(?P<id>\d+)/$', invoice_details),
    )

    #views.py
    @login_required 
    def invoice_details(request, id=1):
            clients_list = Client.objects.filter(pk=id)
        invoices_list = Invoice.objects.filter(pk=id) 
        return render_to_response(('invoice_details.html
', locals()), {'clients_list': clients_list, 'invoices_list': invoices_list }, context_instance=RequestContext(request))

    @login_required 
    def clients_details(request, id=1):
       clients_list = Client.objects.filter(pk=id) 
       return render_to_response(('clients_details.html', locals()), {'clients_list':   clients_list}, context_instance=RequestContext(request))

#invoice_details.html
{% extends "base.html" %}

{% block content %}
<h2>Invoice Details</h2>
<div id="horizontalnav">
  <a href="/index/add_invoice">Add an Invoice</a>
  <a href="/index/work_orders">Add a Work Order</a>
  <a href="/index/add_payment">Add Payment</a>
</div>
<ul STYLE="border: 1px solid;float:left;padding:15px; width: 400px;">
<img src="{{ MEDIA_URL }}images/c2duo.png" border="0" STYLE="text-alignment:left">
<p STYLE="margin-left:340px; margin-top:0px; COLOR:blue; Font-family:ARIAL">INVOICE
  <div id="list">

     {% for invoice in invoices_list %}
       {{invoice.client}}
       <p style="text-align: right;">INVOICE # {{invoice.invoice_no}}<br/>
       {{invoice.contract_info}}<br/>
       {{invoice.date}}<br/>
       {% for invoice in invoice.work_orders.all %}
         {{invoice}}<br/>
       {% endfor %}
     {% endfor %}
     {% for clients in clients_list %}
       {{clients.company}}<br/>
       {{clients.postcode}}<br/>
     {% endfor %}

    </div>
</ul>
{% endblock %}
+1  A: 

Are you sure the clients_list variable is getting a valid value? The filter(pk=id) is the same both for the client and invoice filters. Try inserting a

  print clients_list

right after the line with the filter call to see what it contains.

kaktuslampan
The thing is I actually used id initially for clients first from a different view. This actually worked and I called id=pk. Strange thing why clients do not work and invoice does. The print clients_list did not make it work. Here what I wrote for clients in views. @login_required def clients_details(request, id=1): clients_list = Client.objects.filter(pk=id) return render_to_response(('clients_details.html', locals()), {'clients_list': clients_list}, context_instance=RequestContext(request))
Shehzad009
+1  A: 
 clients_list = Client.objects.filter(pk=id)

id is an invoice id.

EDIT: (based on comments on question)

@login_required 
def invoice_details(request, id=1):
    invoices_list = Invoice.objects.filter(pk=id) 
    client = invoices_list[0].client
    return render_to_response(('invoice_details.html', 
      locals()), {'client': client, 
      'invoices_list': invoices_list }, 
      context_instance=RequestContext(request))

Template: Take out the for loop for clients, put in:

   {{client.company}}<br/>
   {{client.postcode}}<br/>
Lou Franco
You know, This may have worked. I find out once I put in more data
Shehzad009
+1  A: 

strange indentation near clients_list = Client.objects.filter(pk=id)

Andrey Gubarev
If this is an accurate copy/paste, the indention must be fixed.
Andrew Sledge