我有一个名为Agency.agency的模型,其中有一个名为URL的字段,例如我添加了2个代理商,第一个将A的值赋予URL,第二个代理将B的值赋予URL字段。现在我想要覆盖views.py索引中的save_model方法,在添加代理时将Agencie的URL添加到urls.py,如下所示:
(r'^airAgency/A/$','airAgency.views.index'),
(r'^airAgency/B/$','airAgency.views.index'),
所以,当输入http://127.0.0.1:8080/airAgency/A时,索引视图会检测我想访问哪个代理商并将其数据传递给该页面。现在我有2个问题:
答案 0 :(得分:1)
正如其他人所说和/或指出的那样,请勿将特定代理商硬编码到您的urls.py中 通过在网址中添加变量来保持通用和干燥
# urls
(r'^airAgency/(?P<name>[a-zA-Z0-9]+)/$', 'airAgency.views.index'),
# this passes the value there as 'name' to the view for index
#views
def index(request, name): # it needs to know name will be passed to it
some_query = Agency.objects.filter( agency_name = name )
#should also add catching for unknown objects to go to 404s
...
return some_sort_of_http_response
中有更多示例