如何访问python django中的url部分? [英] How to access url part in python django?

查看:186
本文介绍了如何访问python django中的url部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django处理API。我想以下列方式访问我的资源:
{base_url} / employee / {emp_id}



这里 emp_id 不是GET参数。如何在我的视图中访问此参数?有没有任何标准的方式来访问它,而无需手动解析URL?

解决方案

根据您是否使用基于类的视图或是否正在使用标准视图函数,方法不同。



对于基于类的视图,根据您愿意执行的操作(ListView,DetailView,...)通常是不需要解析url,只能在urls.py中指定参数的名称,或直接在类定义中指定参数的名称。



基于类的视图



urls.py

  from mysite.employee.views import EmployeeView 

urlpatterns = patterns('',
...
url(r'^ employee /(? P< pk> [\d] +)/ $',EmployeeView.as_view(),name ='employee-detail'),
...

employee / views.py

  class EmployeeView(DetailV iew):
model = YourEmployeeModel
template_name ='employee / detail.html'

请阅读knbk向您指出的文档,因为您需要导入DetailView



只要简单,您将根据 pk 给定的参数。如果不存在,将抛出404错误。






基于功能的视图中以类似的方式完成:



urls.py

  from mysite.employee.views import EmployeeView 

urlpatterns = patterns('',
...
url(r'^ employee /(?P< ; pk> [\d] +)/ $','mysite.employee.views.employee_detail',name ='employee-detail'),
...

employee / views.py

  from django.shortcuts import get_object_or_404 

def employee_detail(request,pk):
函数中参数的名称是
urls.py
中的正则表达式组名称:'pk'

employee = get_object_or_404(YourEmployeeModel,pk = pk)

#在这里你可以用任何东西替换
return HttpResponse(employee)

我希望这个帮助


I am working on APIs using django. I want to access my resources in following way: {base_url}/employee/{emp_id}.

Here emp_id is not a GET parameter. How can I access this parameter in my view? Is there any standard way to access it without manually parsing URL?

解决方案

Depending on whether you are using class based views or whether you are using standard view functions the method is different.

For class based views, depending on which action you are willing to perform (ListView, DetailView, ...) usually you do not need to parse the url but only to specify the name of the argument in your urls.py or the name of the argument directly inside the class definition.

Class based views

urls.py

from mysite.employee.views import EmployeeView

urlpatterns = patterns('',
    ...
    url(r'^employee/(?P<pk>[\d]+)/$', EmployeeView.as_view(), name='employee-detail'),
    ...
)

employee/views.py

class EmployeeView(DetailView):
    model = YourEmployeeModel
    template_name = 'employee/detail.html'

Please read the doc that knbk pointed out to you as you need to import DetailView

And as simply as that, you will get your employee depending on the pk argument given. If it does not exist a 404 error will be thrown.


In function based views it is done in a similar way:

urls.py

from mysite.employee.views import EmployeeView

urlpatterns = patterns('',
    ...
    url(r'^employee/(?P<pk>[\d]+)/$', 'mysite.employee.views.employee_detail', name='employee-detail'),
    ...
)

employee/views.py

from django.shortcuts import get_object_or_404

def employee_detail(request, pk):
""" the name of the argument in the function is the 
    name of the regex group in the urls.py
    here: 'pk'
"""
    employee = get_object_or_404(YourEmployeeModel, pk=pk)

    # here you can replace this by anything
    return HttpResponse(employee)

I hope this helps

这篇关于如何访问python django中的url部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆