Search code examples
pythondjangoattributeerror

attribute error in Django while making a urls.py file under my app


while i'm trying to create my first project and my first app i'm getting this error:

of course when i removed the url of my app view.py it works perfectly (the home screen)


.... 

  File "C:\Users\pegasus\barakcapitalcom\buildtradeapp\urls.py", line 5, in <module>
    path('buildtradeapp/', views.index, name='index')
                           ^^^^^^^^^^^
AttributeError: module 'buildtradeapp.views' has no attribute 'index'

this is my app urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('buildtradeapp/', views.index, name='index')
]

this is my app views.py:

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

def index(request):
    return HttpResponse("Hello world!")

this is my main project urls.py:


from django.contrib import admin
from django.urls import path, include
from buildtradeapp import views

urlpatterns = [
    path('buildtradeapp/', include('buildtradeapp.urls')),
    path('admin/', admin.site.urls),
]

what did i do wrong?

i'm trying to create my first app and first project, printing hello world using django


Solution

  • You have your app level url pattern wrong. In your app urls.py, change path('buildtradeapp/', views.index, name='index') to path('', views.index, name='index') Also, you need to include your app_name like this app_name = 'buildtradeapp'

    So your app urls.py should now look like this:

    from django.urls import path
    from . import views
    
    app_name = 'buildtradeapp'
    urlpatterns = [
        path('', views.index, name='index'),
    ]