我有一个django应用程序,假设显示发票和客户端。现在出于某些原因当我运行Django服务器时,由于某些原因它只在invoice_list中显示发票数据,但不能在clients_list中显示客户数据。客户端数据确实显示在另一个视图中,但不显示在invoice_details视图中。
编辑:我似乎已经找到了解决方法。 clients_list中的客户端数据确实出现但由于某种原因在错误的URL中。我试着尽可能简单地解释它。
网址
(r'^index/invoice_details/1
此页面上会显示客户详细信息。
(r'^index/invoice_details/2
此页面上会显示客户详细信息。
(r'^index/invoice_details/3
此页面上显示发票。
现在我想要r'^ index / invoice_details / 3来显示写入的内容(r'^ index / invoice_details / 1但因为两个数字都不同它会失败。
以下是一些可能有用的输出。
#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 %}
答案 0 :(得分:1)
您确定clients_list变量是否获得有效值?过滤器(pk = id)对于客户端和发票过滤器都是相同的。尝试插入
print clients_list
在过滤器调用的行之后,查看它包含的内容。
答案 1 :(得分:1)
clients_list = Client.objects.filter(pk=id)
id是发票ID。
编辑:(基于对问题的评论)
@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))
模板:取出客户的for循环,输入:
{{client.company}}<br/>
{{client.postcode}}<br/>
答案 2 :(得分:1)
clients_list = Client.objects.filter(pk=id)
附近的奇怪缩进