Roma
Roma

Reputation: 5

URLs Python Issues

I put this function in views.py:

def index(request):
    return HttpRequest('Hello Ahmed')

and I include it in urls.py like this:

from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
    path('', views.index, name='index'),
    path('admin/', admin.site.urls),
]

But when I go to the URL it gives me this issue:

 TypeError at /
__init__() takes 1 positional argument but 2 were given
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 2.0.1

Upvotes: 0

Views: 343

Answers (2)

Alasdair
Alasdair

Reputation: 308849

Your view should return a response, not a request.

from django.http import HttpReponse

def index(request):
    return HttpResponse('Hello Ahmed')

Upvotes: 1

Daniel Julian
Daniel Julian

Reputation: 117

instead of this:

urlpatterns = [
    path('', views.index, name='index'),
    path('admin/', admin.site.urls),
]

try this:

urlpatterns = [
    path(r'^$', views.index, name='index'),
    path('admin/', admin.site.urls),
]

Upvotes: 0

Related Questions