Django NameError: name 'views' not defined

I work through this quick site development tutorial with Django.

I watched him closely (as far as I can see), but when I try to view the index page I get the following error:

 NameError at /name 'views' is not defined Exception location: \tuts\urls.py in <module>, line 12 

Here urls.py :

 from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), url(r'^$', views.index, name='index'), ) 

Here views.py :

 from django.shortcuts import render # Create your views here. def index(request): items = Item.objects.order_by("-publish_date") now = datetime.datetime.now() return render(request,'portfolio/index.html', {"items": items, "year": now.year}) 

And here models.py :

 from django.db import models # Create your models here. class Item(models.Model): publish_date = models.DateField(max_length=200) name = models.CharField(max_length=200) detail = models.CharField(max_length=1000) url = models.URLField() thumbnail = models.CharField(max_length=200) 

I also have a basic index.html template. Looking around, I think I need to import my look somewhere.

But I'm completely new to Django, so I have no idea. Any ideas?

+6
source share
1 answer

Line error

  url(r'^$', views.index, name='index'), #----------^ 

Here, views not defined, therefore, an error. You need to import it from your application.

in urls.py add a line

 from <your_app> import views # replace <your_app> with your application name. 
+14
source

Source: https://habr.com/ru/post/971964/


All Articles