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'),
]
Related
thats my app urls
from django.urls import path
from . import views
urlpatterns = [
path('', views.tasklist, name='tasks'),
]
thats my projects urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('list.urls')),
]
thats views urls
from django.shortcuts import render
from django.http import HttpResponse
def tasklist(request):
return HttpResponse('TO DO List')
I tried a lot of things but none of them worked
you have to add your app into INSTALLED_APPS list in settings.py
app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('hello', views.index, name='index')
]
project urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello, World!")
I've re-watched the tutorial several times and still can't find the error.
Since your project's urls.py includes hello using the prefix 'hello/' in
path('hello/', include("hello.urls"))
and your app's urls.py also has hello as the path in
path('hello', views.index, name='index')
you will need to access /hello/hello in your browser to get to a view that doesn't 404.
Django's technical 404 page (the one with the yellow background) should show the available paths -- if you don't get the technical 404 page, ensure you have the DEBUG setting turned on.
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.
In views.py
from django.http import HttpResponse
def home_page(request):
return HttpResponse("<h1>Hello, World</h1>")
In urls.py
from django.contrib import admin
from django.urls import path
from .veiws import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
Starting development server at http://127.0.0.1:8000/
Your import says .veiws instead of .views
I was trying to call one html file from another via Django.
I actually have no errors inside my project(It works). But when I am trying to call the second html file the page refreshes only. Here is my code, thanks:D. My projects name is information and the name of my app is basic.
basic.views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'basic/index.html')
def customer(request):
return render(request,'basic/customer.html')
basic.urls.py:
from django.urls import path
from basic import views
app_name='basic'
urlpatterns=[
path('',views.index,name='index'),
path('',views.customer,name='customer'),
]
information.urls.py:
from django.contrib import admin
from django.urls import path,include
from basic import views
urlpatterns = [
path('',views.index,name="index"),
path('admin/', admin.site.urls),
path('',include('basic.urls'))
]
The problem is that you have two paths that match exactly the same path. So that means each time you make a request with an empty path, the first one is used.
You should make use of non-overlapping paths, for example:
# basic/urls.py
from django.urls import path
from basic import views
app_name='basic'
urlpatterns=[
path('', views.index, name='index'),
path('customer/', views.customer, name='customer'),
]
and in your information/urls.py:
# information/urls.py
from django.contrib import admin
from django.urls import path,include
from basic import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('basic/', include('basic.urls'))
]