首页 > 解决方案 > Django,2.0.4 路径不起作用,如何解决?

问题描述

今天第一次安装了 django 的第二个版本,被这个狗屎震惊了,拿走了并删除了使用 url 的正常路由 我知道有一个 re_path 但这不是它!

一般来说,一切都是按照django的说明完成的,但它不起作用,没有看到任何巧合!

错误

Using the URLconf defined in src.urls, Django tried these URL patterns, in this order:

    admin/
    /
    ^media\/(?P<path>.*)$

The empty path didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

主urs.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
    path('/', include("someapi.urls")),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

我的应用程序中的网址

from django.contrib         import admin
from django.urls            import path
from .views                 import handle_verification, send_message
from django.conf.urls       import include, url

urlpatterns = [
    path('', handle_verification, name='handle_verification'),
    path('send/<str:recipient>/<str:txt>/', send_message, name='send_message'),
]

我的看法

import random
from django.core.paginator  import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts       import render, get_object_or_404, redirect
from django.http            import JsonResponse, Http404, HttpResponse
import requests
import json

def handle_verification(request):
    print("Handling Verification.")
    if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
        if not request.args.get("hub.verify_token") == "my_voice_is_my_password_verify_me":
            return "Verification token mismatch", 403
        return request.args["hub.challenge"], 200
    context = {good:"I love HENTAI"}
    return render(request, 'home.html', context)

def send_message(request, token, recipient, text):
    if request.method == 'POST':
        data = request.POST
        recipient = data.get("recipient")
        text = data.get("text")
        print(payload)
        r = requests.post("https://graph.facebook.com/v2.6/me/messages",
        params={"access_token": token},
        data=json.dumps({
          "recipient": {"id": recipient},
          "message": {"text": text}
        }),
        headers={'Content-type': 'application/json'})
        if r.status_code != requests.codes.ok:
            print(r.text)
            return Http404
    else:
        return HttpResponse('Got')

标签: pythondjango

解决方案


/如果您想获得没有附加路径的页面,请不要写。例如:http://example.com/

代替:

path('/', include("someapi.urls")),

类型:

path('', include("someapi.urls")),

此外,django 需要应用程序的名称,以防您想包含应用程序。在这里阅读

记住!标准路径/不是

你输入额外/的,最后你得到//


推荐阅读