姜戈问题
我有一个 Django 应用程序,应该显示发票和客户。现在,由于某些原因,当我运行 Django 服务器时,由于某些原因,它只显示invoice_list 中的发票数据,但无法显示clients_list 中的客户数据。客户数据确实显示在另一个视图中,但不在invoice_details 视图中。
编辑:我似乎已经解决了一些问题。 client_list 中的客户端数据确实出现,但由于某种原因出现在错误的 url 中。我尝试尽可能简单地解释它。
urls
(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 %}
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 %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
id 是发票 ID。
编辑:(基于对问题的评论)
模板:取出客户端的for循环,放入:
id is an invoice id.
EDIT: (based on comments on question)
Template: Take out the for loop for clients, put in:
您确定clients_list变量正在获取有效值吗?客户端和发票过滤器的过滤器(pk=id)是相同的。尝试
在包含过滤器调用的行后面插入一个,看看它包含什么。
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
right after the line with the filter call to see what it contains.
clients_list = Client.objects.filter(pk=id)
附近有奇怪的缩进strange indentation near
clients_list = Client.objects.filter(pk=id)