【问题标题】:Django above version 2.1.5: how to set urls.py to allow get method to work?Django 2.1.5 以上版本:如何设置 urls.py 以允许 get 方法工作?
【发布时间】:2019-06-05 00:06:57
【问题描述】:

我想重复使用相同的模板“createchapter.html”。 换句话说,我想链接到“createchapter/?step=1.html”、“createchapter/?step=2.html”、“createchapter/?step=3.html”。

我使用的是 Django 2.1.5,所以学习的资源或问答很少。

在 urls.py 中

urlpatterns = [
(.....)
    url(r'^createbook/$',viewsSentence.createbook),
    url(r'^createchapter/<int:step>$',viewsSentence.createchapter),

]

在views.py中

@csrf_exempt
    @csrf_protect
def createchapter(request,path):


   if request is not None:
      if request.GET.get('step') is None:
         context1 = {
            'books': book.objects.order_by('titleOrigin'),
            'step': 1,
         }

         #useless: return HttpResponse('/?step=1',context1)
         redirect("createchapter.html",context1,step=1)

      elif request.GET.get('step') == 2:
         context2 = {
            'step': 2,
         }
         redirect("createchapter.html", context2,step=2) 

在 createchapter.html 中

{% if step == 1 %}
      <form>
          {% csrf_token %}
        <div class="form-group">
           <i class="fas fa-forward"></i>
          <label for="select-books">Step 1: select a book</label>
          <select class="form-control" id="select-books">
                {% for book in books %}
                <option value="{{ book.id }}">{{ book.titleOrigin }}</option>
                {% endfor %}

          </select>
        </div>
      </form>
        <div class="d-flex justify-content-center">
                <i class="fas fa-arrow-alt-circle-right"><a id="createchapter-step2">Step 2</a></i>
        </div>
    {% elif step == 2%}
    <div class="form-group">
           <i class="fas fa-forward"></i>
          <label>Step 2: set numbers of chapters</label>
    </div>
    {% endif %}

这是我如何调用第二页(我使用 ajax):

 $("#createchapter-step2").click(function(){

        var selectedbookId = $("#select-books option:selected").val();

        $.ajax({
            url: '/createchapter/?step=2',
            type:'get',
            data:{
                'selectedbookId':selectedbookId,
                csrfmiddlewaretoken: $( "#csrfmiddlewaretoken" ).val(),
            },
        });
    });

错误信息是:

    Page not found (404)
    Request Method:   GET
    Request URL:  http://127.0.0.1:8000/createchapter/?step=1/

【问题讨论】:

  • step=1 无法按照您的代码工作。但只需将其保留为127.0.0.1:8000/createchapter/1
  • @VineethSai,我也试过 JPG 的方法。谢谢你。在我的 urls.py 中,我写了“^createchapter//$”,错误消息是“当前路径 createchapter/1/,与这些都不匹配。”。调试说我的请求 URL 是 127.0.0.1:8000/createchapter/1 。

标签: python django url redirect


【解决方案1】:

实际上你正在做的是在http://127.0.0.1:8000/createchapter/?step=1 中使用url querystring。您不需要在 url(step 部分)中定义它。所以应该是这样的:

url(r'^createchapter/$',viewsSentence.createchapter),

然后获取url查询字符串参数,可以这样尝试:

from django.shortcuts import render


@csrf_exempt
@csrf_protect
def createchapter(request):
   step = request.GET.get('step')  # it will fetch the step from url

   if step == "2":
     context = {
        'step': 2,
     }

   else:
     context = {
        'books': book.objects.all().order_by('titleOrigin'),
        'step': 1,
     }

   return render(request, "createchapter.html",context)

【讨论】:

  • 我已经应用了你的方法。非常感谢。第一页成功,而第二页没有。 python 终端将 request.GET.get('step') 正确捕获为“2”,但渲染没有生效。终端显示“[10/Jan/2019 15:19:02]”GET /createchapter/?step=2&selectedbookId=2 HTTP/1.1“200 1901”。浏览器保持沉默。
  • 对不起,我犯了一个小错误。 step的值是一个字符串,所以应该和"2"匹配。我已经更新了答案
  • 谢谢;我实际上注意到了这一点,并在您在这里发表评论之前将其更改为字符串,但浏览器仍然保持沉默。我还通过添加有关 .js 文件的内容来编辑我的问题。
  • 也许你可以这样尝试而不是做一个ajax请求:window.location = location.protocol + "//" + location.hostname + "/createchapter/?step=2";
【解决方案2】:

你输入了错误的网址,应该是http://127.0.0.1:8000/createchapter/1/

【讨论】:

  • 我也试过你的方法。谢谢你。在我的 urls.py 中,我写了“^createchapter//$”,错误消息是“当前路径 createchapter/1/,与这些都不匹配。”。调试表明我的请求 URL 是 127.0.0.1:8000/createchapter/1
猜你喜欢
  • 1970-01-01
  • 2021-05-29
  • 2019-08-01
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
相关资源
最近更新 更多