Django: error in configuring url's

3

I'm Using

  • django in its version: 1.11.4

  • python 3.6 in linux debian

I have the following files configured in settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
    'about',
    'contact',
   ]

urls.py is set up like this:

from django.conf.urls import url
from django.contrib import admin
from home import views
from about import views
from contact import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^home/$', views.Home, name='home'),
    url(r'^about/$', views.About, name='about'),
    url(r'^contact/$', views.Contact, name='contact'),
]

home / views.py with the following code

from django.shortcuts import render
from django.http import HttpResponse

def home(request):
    return render(request, 'Home.html')

contact / views.py I wrote it that way

from django.shortcuts import render
from django.http import HttpResponse
from .models import Contact


def contact(request):
    contacts = Contact.objects.all()
    return render(request, 'Contact.html', {'contacts': contacts})

Finally I have the folder templates with its files:

  • home.html,
  • about.html,
  • contact.html

When executing the command

python manage runserver 

the console displays the following error:

File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

    url(r'^home/$', views.Home, name='home'),
    AttributeError: module 'contact.views' has no attribute 'Home'

I have made different configurations, I can not find what the error is

    
asked by Developer 08.03.2018 в 06:49
source

2 answers

0

Is not it a subject of capitals? You have in home view redirection to Home.html (would not it be home.html?). Then in urls.py you say view.Home and your function is home. Also, I do not know if it is correct to import the three views with the same name views. In the course that I followed django I did import views as viewhome and in the urls.py do viewhome.myFunction.

    
answered by 08.03.2018 / 11:34
source
2

I see some problems.

  • Your views have been defined in lowercase, not capitalized

  • You are importing all views with the same name:

    from home import views
    from about import views
    from contact import views
    

    If you do it that way then the last views is the one that prevails (that of your application contact ). Try to do it with specific names:

    from home import views as home_views
    from about import views as about_views
    from contact import views as contact_views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^home/$', home_views.home, name='home'),
        url(r'^about/$', about_views.about, name='about'),
        url(r'^contact/$', contact_views.contact, name='contact'),
    ]
    
  • Having this clear, the complete code of your URLs should be:

    from django.conf.urls import url
    from django.contrib import admin
    from home import views as home_views
    from about import views as about_views
    from contact import views as contact_views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^home/$', home_views.home, name='home'),
        url(r'^about/$', about_views.about, name='about'),
        url(r'^contact/$', contact_views.contact, name='contact'),
    ]
    
        
    answered by 08.03.2018 в 16:53