Compoot
Compoot

Reputation: 2387

Django URLs file error

I have the following urls.py file in a Django project, and I am getting an error which I assume is relating to the latest syntax relating to urls and paths.

The code I have in the urls file which is url.py in the mysite (outermost directory) is:

from django.urls import path
from django.conf.urls import url, include 


urlpatterns = [
    path(r'^$', include('aboutme.urls')),

]

The error message is:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^$
The empty path didn't match any of these.

In the actual app (website folder) which is called 'aboutme', the urls.py file looks like this:

from django.urls import path
from django.conf.urls import url, include 
from .import views #this is the main thing we are going to be doing ....returning views!

urlpatterns = [

        path(r'^$', views.index,name='index'),

]

Can anyone shed any light on the correct syntax or what I am doing wrong?

UPDATE:

I also went back and tried to update the main mysite's url.py file to include the admin commands (which were in the previously working version). The code and resultant error are also shown below:

Tried this code:

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


urlpatterns = [
    path('admin/', admin.site.urls),
    path(r' ', include('aboutme.urls')),

]

Error

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The empty path didn't match any of these.

Upvotes: 1

Views: 4188

Answers (1)

Dalvtor
Dalvtor

Reputation: 3286

Remove the ^ and $ in the urls.py files.

from django.urls import path
from django.conf.urls import url, include 

urlpatterns = [
    path(r'', include('aboutme.urls')),
]

And in your app urls.py:

from django.urls import path
from django.conf.urls import url, include 
from .import views #this is the main thing we are going to be doing 

app_name="myappname"
urlpatterns = [
    path(r'', views.index,name='index'),
]

In django 2.0 they are not needed anymore if you are using path().

Related link: https://code.djangoproject.com/ticket/28691

Upvotes: 3

Related Questions