Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...