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

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

I have written this code in main/urls.py file

code in logs/urls.py is below:-

from django.conf.urls import url
from . import views

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

and i am accessing http://localhost:8000/logs

Error:- NameError name 'logs' is not defined

Bhulawat Ajay
  • 287
  • 2
  • 3
  • 16

3 Answers3

0

You need to import the module you are actually referencing before adding it to your URLs file.

Assuming logs is in your import path:

import logs
Azsgy
  • 3,139
  • 2
  • 29
  • 40
0

Usually you would use a string with include so that you don’t have to import the module;

url(r'^logs/', include('logs.urls')

Also, you should remove the slash from the end of your regex for the index view. The dollar marks the end of the string, so r'^$/' will never match.

url(r'^$', views.index, name='index'),
Alasdair
  • 298,606
  • 55
  • 578
  • 516
0

Please check the docs. It should help!

include() function takes a list/tuple of url(), path(), re_path() and includes them to django routing system.

It also requires app's name: you can define it in include(("enter here your app's name", app.urls)) or in an app itself, in urls.py at a modular level. app_name = 'enter here your apps'name '

But only include() isn't enough, you have to define it in path() or smth that is used for routing (e.g re_path(), url() (deprecated in django 2.0))

For full details: include() function.

So, the full poiting to app's urls look so:

 from django.conf.urls import url, include
 from . import views
 from . import app

   urlpatterns = [
     url(r'^', include(("app", app.urls)), name='index'),
   ]

I recommend you to use path() and re_path() if you use Django 2.0 >


I noticed that you use regex incorrectly little bit

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

You type $ before /, it's incorrect, $ means the end of expression and must be used in the end of expression.


Unfortunately I can't tell you all the details in the post. You should read Django docs. Go through django tutorial for start.

I'm sure it will help!

joe513
  • 96
  • 1
  • 4