django template url from another app

好久不见. 提交于 2020-01-11 06:21:08

问题


I have to be missing something silly. I have a {% url %} in a template where the action is from another app. It isn't working but I have no clue if there is something different about using view functions from other apps or if I am just doing something silly.

call/template/call/file.html

    <form action="{% url 'upload_image' %}"></form>

picture/urls.py

from .views import PictureList, PictureCreate, PictureDetail, PictureUpdate, PictureDelete, upload_image

...
url(r'^upload_image/$', upload_image, name='upload_image'),
...

picture/view.py

def upload_image( request ):
    print 'IN IMAGE UPLOAD'
    print request

All I ever get is:

NoReverseMatch at /call/4/

Reverse for 'upload_image' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

回答1:


When calling reverse() on an URL that comes from a different application, you should use its "namespaced" version, like so:

{% url 'app_name:app_url' %}

In your specific case, that translates to:

{% url 'picture:upload_image' %}


来源:https://stackoverflow.com/questions/26812980/django-template-url-from-another-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!