如何传入查询字符串网址.py用什么?key=以djang表示的值(&V)

2024-09-30 18:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

django文件指出:

Now in your time on the web you may have come across such beauties as “ME2/Sites/dirmod.asp?sid=&type=gen&mod=Core+Pages&gid=A6CD4967199A42D9B65B1B”. You will be pleased to know that Django allows us much more elegant URL patterns than that.

但是,我试图在传递查询字符串时考虑到这个模式。如何在django的内部传递key=value对呢网址.py在

还要注意的是,这是用于同时使用Angular和Django的项目。在

目前我的角度模式正在查询字符串中显示出来 一、 E.http://localhost/data/?key1=value1&key2=value2

如果你对如何处理$http.post,我会接受任何建议。在

更新:

这是我的视图.PY在

#THREE PARAMETERS: storyElementName, facet, variants(can be multiple)
def storyElements_getOrCreate(request, storyElementName, facet, variants):
contData = lsContinuityData.ContinuityData()
if request.method == "POST":
    storyElementName = request.POST.get("storyElementName")
    facet = request.POST.get("facet")
    for key in request.POST:
        variants = request.POST.getlist("variants")
    storyElement = contData.storyElement(storyElementName, facet=facet, variants=variants)
    return HttpResponse(json.dumps(storyElementName, facet, variants), content_type="application/json")
else:
    storyElementGroups = contData.getStoryElements()
    if len(storyElementGroups):
        storyElements = [storyElement.toJSON() for storyElement in storyElementGroups]
        return HttpResponse(json.dumps(storyElements), content_type="application/json")

这是我的角度控制器

^{pr2}$

希望这会有点帮助。我传递了多个参数,但是,我可能做错了。在


Tags: djangoinjsonthatrequesttypebepost
3条回答

我已经意识到出了什么问题。在

当使用$http的POST从AngularJs发送数据时,它将内容类型为=“application/json”的数据发送到服务器。Django不理解这种格式。在

解决方案是使用以下配置更改内容类型标头:

    app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';}]);

您可以在任何已定义的URL上执行此操作。在

因此,在与http://localhost/data/对应的视图中,假设DataView(假设是CBV):

class DataView(View):

    def get(self, request, *args, **kwargs):

        # To get key=value params
        params = request.GET  # This should return a dict {'key': 'value'}

{Django在这个模式中应该拥有更多的Django。在

url(r'^data/?P<val1>\d+/?P<val2>\d+$', 'Project.views.my_project.page', name="my_project")

然后可以检索视图中的值,如下所示

^{pr2}$

d+接收一个或多个十进制数字。在

然后,您的url将具有如下所示的传递值

http://localhost/data/val1/val2

相关问题 更多 >