I am newbie in django framework
My project urls.py has the following code
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^rango/$',include(rango.urls)), #my test appliaction
url(r'^admin/', include(admin.site.urls)),
)
I have created an app called rango , that i have imported in the main project url file.
Now it throwing the following error, when I am trying to access url .../rango/
Exception Value: name 'rango' is not defined
I can see that in that python path is not correctly set up.
This is the directory structure of my project
project/
project/__init__.py
project/urls.py
rango/__init__.py
valuable advice required.
url(r'^rango/$',include(rango.urls)),
should read:
url(r'^rango/',include('rango.urls')),
No $ and use quotes 'rango.urls'. Because rango without the quotes is not defined.
Related
I am completely new in Django and currently learning it from Udemy course.
First, I created my Django project and then created my application. I have inserted my application on settings.py in INSTALLED_APPS of project folder and then re run the the server as well.
Now, I have created a view in my application folder and want to use that in url.py of project folder. I could not able to import it as its showing that package not found.
Please see my program structure below:
I have written the code on pycharm IDE. My code in url.py is below
from django.contrib import admin
from django.urls import path
from firstApp import views
urlpatterns = [
path('^$', views.index, name='index'),
path('admin/', admin.site.urls),
]
Try changing path('^$', views.index, name='index') to path('^$', include('firstApp.urls'), namespace='index')
And you need to import include from django.urls
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.
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
I am working through a (mycodesmells) django tutorial and towards the bottom under "ADDING TO THE PROJECT" it says to update the url.py file with the code that it gives.
Once I update that file it crashes the server and gives me the error.
"No module named 'django_simple.todo'
I looked in SO posts and template view and redirect were mentioned in the following post.
1. Does this mean its deprecated?
2. How do i fix or adjust the code for Django 1.1 and Python 3
Templateview
from django.conf.urls import include, url
from django.contrib import admin
from django_simple.todo import views as todo_views
admin.autodiscover()
urlpatterns = [
url(r'^$', todo_views.index),
url(r'^admin/', include(admin.site.urls)),
]
Make sure you have an __init__.py file in the "todo" folder. That lets Python know it's a module.
Also make sure you have django_simple.todo in your Django apps list in your settings.py.
I have a django project mysite
and a djano app in a directory (proto) called http
and here is me url setting:
mysite/mysite/urls.py:
import proto
urlpatterns = patterns('',
url(r'^data/',proto.http.views.DataList.as_view(),name='data')
)
mysite/proto/http/urls.py:
from django.conf.urls import patterns, include, url
from proto.http import views
urlpatterns = patterns('',
url(r'^data/$',views.DataList.as_view(),name='data-list'),
)
So I can visit : http://127.0.0.1:8000/data/
When I reviewmy code ,I feel weird why I have to set the url twice?
But if I comment out the url pattern in mysite/proto/http/urls.py
#urlpatterns = patterns('',
# url(r'^data/$',views.DataList.as_view(),name='data-list'),
#)
I got 500 error
ImproperlyConfigured at /data/
The included urlconf '<module 'proto.http.urls' from '/Users/Winsome/mysite/proto/http/urls.pyc'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
What is the correct way to set url pattern ??
I am learning to build a Django app named rango from the site http://www.tangowithdjango.com/ , but things are bit different as I am using Django 1.9 which is not same as in the tutorial, so I am facing difficulty in running the app. Whenever I run the server, it gives error as
NameError: name 'rango' is not defined
I have included rango in the INSTALLED_APPS list. Here is my master urls.py file's code:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include(rango.urls)),
]
And here is my local urls.py file (rango's urls.py) that is kept within the /rango folder:
from django.conf.urls import patterns, url
from rango import views
urlpatterns=patterns('',
url(r'^$',views.index,name='index'),)
So why is my rango app not being located?
This is happening because the rango.urls has to be in quotes. So your master urls.py should look like this -
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^rango/', include('rango.urls')),
]