Django: Support for string view arguments to url() is deprecated and will be removed in Django 1.10

≯℡__Kan透↙ 提交于 2019-12-29 07:22:07

问题


New python/Django user (and indeed new to SO):

When trying to migrate my Django project, I get an error:

RemovedInDjango110Warning: Support for string view arguments to url() is deprecated 
and will be removed in Django 1.10 (got main.views.home). Pass the callable instead.   
url(r'^$', 'main.views.home')

Apparently the second argument can't be a string anymore. I came to create this code as it is through a tutorial at pluralsight.com that is teaching how to use Django with a previous version (I'm currently working with 1.9). The teacher instructs us to create urlpatterns in urls.py from the views we create in apps. He teaches us to create a urlpattern such as the following:

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', 'main.views.home')
]

to reference

def home(request):
    return render(request, "main/home.html",
                    {'message': 'You\'ve met with a terrible fate, haven\'t you?'}) #this message calls HTML, not shown, not important for question

in the views.py of an app "main" that I created.

If this method is being deprecated, how do I pass the view argument not as a string? If I just remove the quotes, as shown in the documentation (https://docs.djangoproject.com/en/1.9/topics/http/urls/), I get an error:

NameError: name 'main' is not defined

I tried to "import" views or main using the code presented in this documentation:

from . import views

or

from . import main

which gave me:

ImportError: cannot import name 'views'

and

ImportError: cannot import name 'main'

I believe I've traced this down to an import error, and am currently researching that.


回答1:


I have found the answer to my question. It was indeed an import error. For Django 1.10, you now have to import the app's view.py, and then pass the second argument of url() without quotes. Here is my code now in urls.py:

from django.conf.urls import url
from django.contrib import admin
import main.views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', main.views.home)
]

I did not change anything in the app or view.py files.

Props to @Rik Poggi for illustrating how to import in his answer to this question: Django - Import views from separate apps




回答2:


You should be able to use the following:

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

from main import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home)
]

I'm not absolutely certain what your directory structure looks like, but using a relative import such as from . import X is for when the files are in the same folder as each other.




回答3:


You can use your functions by importing all of them to list and added each one of them to urlpatterns.

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

from main.views import(
   home,
   function2,
   function3,
)

urlpatterns = [
   url(r'^admin/', admin.site.urls),
   url(r'^$', home),
   url(r'^$', function2),
   url(r'^$', function3),
]


来源:https://stackoverflow.com/questions/34096424/django-support-for-string-view-arguments-to-url-is-deprecated-and-will-be-rem

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