Django 查看错误 - NoReverseMatch

Posted

技术标签:

【中文标题】Django 查看错误 - NoReverseMatch【英文标题】:Django View Error - NoReverseMatch 【发布时间】:2017-10-22 00:32:31 【问题描述】:

我是 django 新手,我正在尝试解决 NoReverseMatch 问题。我认为这与观点有关,但我对此并不陌生。

代码来自几年前流行的样板代码库。请注意:我已经尝试阅读有关堆栈溢出的每个答案并且已经卡了几个小时。

任何帮助将不胜感激

主 urls.py

from django.conf.urls import include, url
from django.contrib import admin


urlpatterns = [
    url(r'^login/', include('shopify_app.urls')),
    url(r'^', include('home.urls'), name='root_path'),
    url(r'^admin/', admin.site.urls),
]

应用内的urls.py

from django.conf.urls import url
from shopify_app import views

urlpatterns = [
        url(r'^$', views.login, name='shopify_app_login'),
        url(r'^authenticate/$', views.authenticate, name='shopify_app_authenticate'),
        url(r'^finalize/$', views.finalize, name='shopify_app_finalize'),
        url(r'^logout/$', views.logout, name='shopify_app_logout'),
]

应用内的views.py

from django.shortcuts import redirect, render
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.conf import settings
import shopify

def authenticate(request):

    shop = request.GET.get('shop')

    print('shop:', shop)

    if shop:
        scope = settings.SHOPIFY_API_SCOPE
        redirect_uri = request.build_absolute_uri(reverse('shopify_app.views.finalize'))
        permission_url = shopify.Session(shop.strip()).create_permission_url(scope, redirect_uri)
        return redirect(permission_url)

    return redirect(_return_address(request))



def finalize(request):
    shop_url = request.GET['shop']
    try:
        shopify_session = shopify.Session(shop_url)
        request.session['shopify'] = 
            "shop_url": shop_url,
            "access_token": shopify_session.request_token(request.REQUEST)
        

    except Exception:
        messages.error(request, "Could not log in to Shopify store.")
        return redirect(reverse('shopify_app.views.login'))

    messages.info(request, "Logged in to shopify store.")

    response = redirect(_return_address(request))
    request.session.pop('return_to', None)
    return response

错误

NoReverseMatch at /login/authenticate/
    Reverse for 'shopify_app.views.finalize' not found. 'shopify_app.views.finalize' is not a valid view function or pattern name.
    Request Method: GET
    Request URL:    http://localhost:8000/login/authenticate/?csrfmiddlewaretoken=zEwwHeTfxK7apbAp3dSxsehsafxqjSgEM4t&shop=piepiedev.myshopify.com&commit=Install
    Django Version: 1.11.6
    Exception Type: NoReverseMatch
    Exception Value:    
    Reverse for 'shopify_app.views.finalize' not found. 'shopify_app.views.finalize' is not a valid view function or pattern name.

源代码/文件结构- https://github.com/Shopify/shopify_django_app

类似的问题,但不工作的解决方案 - https://github.com/Shopify/shopify_django_app/issues/13

【问题讨论】:

你能在应用内编辑视图吗? 【参考方案1】:

在身份验证(请求)内部更改:

redirect_uri = request.build_absolute_uri(reverse('shopify_app:shopify_app_finalize'))

【讨论】:

刚试过。我收到'shopify_app' is not a registered namespace 是否有任何视图需要修改? @Alexander Tyapkov @NewPieGuy 您是否将 shopify_app 添加到您的应用设置中? 嗯。如果您指的是 root settings.py - 是的,它就在那里。 @NewPieGuy 你能把链接发送到应用程序仓库吗?我找不到它 我刚刚将url(r'^login/', include('shopify_app.urls', namespace="shopify_app")), 添加到root url.py,这似乎可以解决问题.. 谢谢!这会破坏其他任何事情吗?我克隆了这个仓库——github.com/aorzh/shopify_django_app

以上是关于Django 查看错误 - NoReverseMatch的主要内容,如果未能解决你的问题,请参考以下文章

错误:查看 Django 中的更改时

django查看完整性错误

我想知道为啥当我发送一个数组以使用 jquery ajax (django,jquery) 查看时发生错误

解决django TemplateDoesNotExist错误

如何查看生成到 django 模板变量中的异常?

Django/Ajax 发送“无”值来查看