-1

Please assist figuring out solution to this error.

courses/ views.py

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

from .models import Course

def course_list(request):
    courses = Course.objects.all()
    return render(request, 'courses/course_list.html',{'courses':courses})

admin/ views.py

from django.shortcuts import render

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

admin urls.py

from django.contrib import admin
from django.urls import path
from courses import views
from django.conf.urls import include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('admin/', views.hello_world()),
    path('courses/', include('courses.urls')),
    path('courses/', views.course_list(), name='listofcourses'),
]

courses/ urls.py

from django.urls import path
from . import views

urlpatterns=[
path('',views.course_list, name="listofcourses"),
]

Now on running server, i am getting this error

hello_world() missing 1 required positional argument: 'request'

I wish to publish homepage on running server at 127.0.0.1:8000 and courses page on 127.0.0.1:8000/courses

Thank you in advance

Vishesh
  • 11
  • 2
  • 1
    Replace `views.hello_world()` with `views.hello_world`. You need to pass the view function itself to the URL config, not the result of the function. Note also that you've used the same URL path `admin/` as the definition above, so yours will never be reached because the one above it will match first. – solarissmoke Dec 01 '18 at 08:00
  • Tried `views.hello_world` it gives another error **module 'learning_site.views' has no attribute 'course_list'** – Vishesh Dec 01 '18 at 08:03

1 Answers1

2

You are calling(executing) the views in urls, which shouldn't do. You can do it like this:

path('hello-world/', views.hello_world),   # don't use 'admin/' because you already have url configured against this path.
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • Tried `path('hello_world/, views.hello_world)` it gives another error **module 'learning_site.views' has no attribute 'course_list'** _"course_list"_ is actually an attribute of **courses.views** – Vishesh Dec 01 '18 at 08:06
  • I think its related to `path('courses/', views.course_list(), name='listofcourses'),` in admin urls. Remove this line and try again. – ruddra Dec 01 '18 at 08:09
  • Tried it. The homepage is not visible now. at http://127.0.0.1:8000 it is giving PAGE NOT FOUND error, but when i tried http://127.0.0.1:8000/courses it is giving desired output the error is in this line of code only but how do i display homepage as per desired output now? – Vishesh Dec 01 '18 at 08:12
  • 1
    got it sorted. Thank you @ruddra – Vishesh Dec 01 '18 at 08:20