Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

Tags:

python

url

django

I know this question has been asked before, but I haven't found an answer that solves my situation.

I'm looking at the Django tutorial, and I've set up the first URLs exactly as the tutorial has it, word for word, but when I go to http://http://localhost:8000/polls/, it gives me this error:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^% [name='index']
^admin/
The current URL, polls/, didn't match any of these.

I'm using Django 1.10.5 and Python 2.7.

Here is the code I have in relevant url and view files:

In mysite/polls/views.py:

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

# Create your views here.
def index(request):
  return HttpResponse("Hello, world. You're at the polls index.")

In mysite/polls/urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
  url(r'^%', views.index, name='index'),
]

In mysite/mysite/urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

What's going on? Why am I getting 404s?

like image 335
HLH Avatar asked Jan 12 '17 04:01

HLH


People also ask

What is URLconf in Django?

A URLconf is similar to a table of contents for our Django-powered web site. It's a mapping between URL patterns and the view functions that need to be called for those URLs. First, we will have to import the view function that we want our server to run when the URL is matched.

What is a URL pattern Django?

In Django, views are Python functions which take a URL request as parameter and return an HTTP response or throw an exception like 404. Each view needs to be mapped to a corresponding URL pattern. This is done via a Python module called URLConf(URL configuration) Let the project name be myProject.

What is URLconf search against?

The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name. For example, in a request to http://www.example.com/myapp/, the URLconf will look for myapp/ .

What is URLs py file in Django?

A request in Django first comes to urls.py and then goes to the matching function in views.py. Python functions in views.py take the web request from urls.py and give the web response to templates. It may go to the data access layer in models.py as per the queryset.


1 Answers

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

like image 94
Panyam Praneeth Reddy Avatar answered Sep 28 '22 04:09

Panyam Praneeth Reddy