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')),
]
Related
I have python version 3.10.1 and django version 4.0
url in project( name = home)`
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('hello.urls')),
path('admin/', admin.site.urls),
]
url in app (name = hello)
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('hello.urls')),
path('admin/', admin.site.urls),
]
views in app
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World")
I tried running server with and without adding 'hello' in setting.py still i get only default page.
stuck from 3 days
You have the same code in urls.py home project as well as in hello app. For Django to use your new view, you need to tell Django the index view is the view you want to display when someone navigates to the site root (home page). So you need to change the urls.py of hello app as:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
In this case, a request to http://localhost:8000/ would route to the index function in the application’s (hello) views.py file.
In the urls.py file in app write the following code:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
i have just now started learning the django basics ,but i have been keeping on facing a problem with the urls & views. i do not get the "Hello world" after i reload or even try adding myapp name in the url
wrote this code for the project url
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('calc.urls')),
path('admin/', admin.site.urls),
]
wrote this for myapp urls
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
wrote this for the views
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Hello world");
what is the mistake i am doing?
link: http://127.0.0.1:8000/calc
You are not setting the correct URL. Also have you added your in installed apps ?
if yes then visit: http://127.0.0.1:8000
or add calc in your url to use http://127.0.0.1:8000/calc
urlpatterns = [
path('calc', views.home, name='home'),
]
Is your myapp urls file inside a folder called calc? If it isn't then try doing something like this:
Change include('calc.urls') to include('\the folder name in which myapp urls is in\.urls')
Then use the http://127.0.0.1:8000 and it should work.
I am trying to do the following thing in urls.py but Django 2.0.5 doesn't seem to support url(). Instead of it, I used path() but still, its throwing invalid syntax error.
Can someone give a clearer picture of path() as it seems to be not supporting regex.
Providing the code here:
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('$', home_page)
path('admin/', admin.site.urls),
]
You miss a ,, and $ is unnecessary
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
Django2 has 2 functions for URLconfs, path(), re_path().
You can use regex paths (regular expression based paths) with re_path(), so remove $ and place , between two consecutive paths.
Note: Let suppose your app name is my_django_app created by python manage.py startapp my_django_app command.
I created a new Django app named my_django_app and tried, it works fine. I have the following code in my urls.py file.
"""my_django_proj URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from my_django_app.views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
References: https://docs.djangoproject.com/en/2.0/topics/http/urls/
django 2.0 - including urls file from an app to the root urls file
Thanks.
If you prefer to use url instead of path, that will work just fine. You just have to import from django.conf.urls instead. So your import statement should look like this:
from django.conf.urls import url
Django says on their documentation page that this feature will likely be deprecated in future versions to re-path, however, url still works fine for me, and I'm running Django 2.0.7... so, I imagine it would work with yours as well. I guess because of this, with Django version 2 and above, it nows decides when it creates the boilerplate project that instead of importing urls from django.conf.urls, it imports path from django.urls. (Note: PATH doesn't allow for regex)
What I typically do, is create an app specific urls.py. In that urls.py I'll import url from django.conf.urls and have my specific app level urls there:
from django.conf.urls import url
from app_name import views # have to import views
urlpatterns = [
url(r'^$', views.index),
url(r'^users$',views.users),
]
Then in the project level urls.py I'll add the include module as so I can link it to my app specific urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
from app_name import views
urlpatterns = [
url(r'^',include('app_name.urls')),
url(r'^admin/', admin.site.urls),
]
(Note: If you have a separate folder in between such that the folder structure looks something like mainproject>apps>app_name>(settings.py, views.py, admin.py etc...) you will have to create an __init__.py file in the apps folder as so Django can recognize the module.
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls)
]
As stated in the above answers, the $ is unnecessary while using path(). You are getting a syntax error due to the comma after admin.site.urls) which should be removed.
So I am following a Django tutorial and I have reached the stage where I add my own URL patterns to the urls.py file.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url((r'^webapp/', include('webapp.urls'))),
url(r'^admin/', admin.site.urls),
]
the problem is that when I run the server Django doesnt even search for the pattern. I'm not sure what I'm doing wrong here
webapp.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Make sure the directory structure is:
app
app
urls.py
webapp
urls.py
You write about a file named webapp.urls.py that is unusable by django
You pass wrong arguments for url().
Wrong:
url((r'^webapp/', include('webapp.urls')))
Right:
url(r'^webapp/', include('webapp.urls'))
Trying to figure out how to setup my own project.
I created a new Django app to make a homepage.
src/home/urls.py:
from django.conf.urls import url
urlpatterns = [
url(r'^$', 'views.index', name='index'),
]
src/home/views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "index.html", {})
src/project/urls.py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', include('home.urls')),
url(r'^admin/', admin.site.urls),
]
src/templates/index.html:
<h1>Hello World</h1>
The reason this isn't in a templates folder inside of the home app is because I want to use this as my base template eventually for all apps and pages
Error reads:
ImportError at /
No module named 'views'
Using python 3.5 and django 1.9
EDIT*** changed to home.views.index
ERROR now reads:
TemplateDoesNotExist at /
index.html
In Site-wide Urls.py do the Following:
from app_name import urls as app_name_urls
from django.conf.urls import include
urlpatterns=[
path('',include(app_name_urls)
]
In App/urls.py
from django.urls import path
from .import views
urlpatterns=[
#your paths go here
]
Make sure you home is a package and you have __init__.py there.
You might also need to change views.index to home.views.index in your urls.py