After linking urls&views "hello world" is not visible in django - python

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.

Related

cant get hello world in django

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'),
]

python: Django Page not found (404)

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.

Django can't map url to view using include method

I have the following urls.py in my project dir,
Main project urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.render_calculator, name='render_calculator'),
path('calculator/', views.render_calculator, name='render_calculator'),
path('disclaimer/', views.render_disclaimer, name='render_disclaimer'),
path('cookiepolicy/', views.render_cookiepolicy, name='render_cookiepolicy'),
path('privacypolicy/', views.render_privacypolicy, name='render_privacypolicy'),
path('dashboard/', views.render_dashboard, name='render_dashboard'),
path('about/', views.render_about, name='render_about'),
path('admin/', admin.site.urls),
path(r'^', include('accounts.urls'))
]
Now I created a new app accounts (I added it to my apps in settings.py) where I would like to store the urls of that app in its own dir like so:
Accounts app urls.py
from . import views
from django.urls import path
urlpatterns = [
path('register/', views.render_register, name='render_register'),
]
Accounts app views.py:
from django.shortcuts import render
def render_register(request, template="register.html"):
return render(request, template)
However, this configuration throws me this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/register
Using the URLconf defined in CFD.urls, Django tried these URL patterns, in this order:
[name='render_calculator']
calculator/ [name='render_calculator']
disclaimer/ [name='render_disclaimer']
cookiepolicy/ [name='render_cookiepolicy']
privacypolicy/ [name='render_privacypolicy']
dashboard/ [name='render_dashboard']
about/ [name='render_about']
admin/
^
The current path, register, didn't match any of these.
Where is the missing piece?
You are using path() with the regex r'^' which is causing your problem.
In order to define a path with a regex, you need to use re_path.
So change it to the following line:
re_path(r'^', include('accounts.urls'))
or you can use
path('', include('accounts.urls'))
Change this.
path(r'^', include('accounts.urls')) to
path('', include('accounts.urls'))

Cannot import name 'views',.Python, Django

I have read many answers in this forum, but they does not solve my problem. I will be very grateful for help.
My file views.py returns this error:
from . import views
ImportError: cannot import name 'views' from '__main__' (C:/Users/tymot/Desktop/weather app/env/Environemnt/the_weather/weather/views.py)
views.py (Environemnt\the_weather\weather)
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html
urls.py (Environemnt\the_weather\weather)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
urls.py (Environemnt\the_weather\the_weather)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('weather.urls')),
templates(the_weather\weather\templates\weather)
only file index.html
Directory
-the_weather
--the_weather
---__init__
---setting
---urls
---wsgi
--weather
---migrations
----__init__
---templates
----weather
-----index
---__init__
---admin
---apps
---models
---tests
---urls
---views
--db
--manage.py
I try use to resolved my problem from __future__ import absolute_import, or homepage import views. I else try copy views.py to directory templates (and modify its code) but unfortunately it not work
You need to separate your views and urls create a new module (file) urls.py in your app, in your case it is weather folder, and add these code there, and remove it from views.py, you can read here about it to understand it better.
Path : the_weather/weather/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
Path : the_weather/weather/views.py
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html template
You would need to put your views.py file in your weather folder. Make sure it is in the right weather folder, I am guessing you have two weather folders. Also make sure these codes are right;
#Path : the_weather/weather/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
#Path : the_weather/weather/views.py
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html template
Why don't you try like this
from .views import index
There may be separate folder of urls and views.py
what you have to do is you have to write
from appname import views
suppose your app name is myapp
you have have to write
from myapp import views
use should create new url.py in weather and write code in views.py
url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
write code in urls.py of your project
from django.urls import path, include
from . import views
urlpatterns = [
path('wheather/',include('wheather'))
]
I had Similar problem, it didn't solved, till I added an empty views.py in root in example, in your case add to Environemnt\the_weather\the_weather
I had the same problem.
In formsdemo\formsdemo\__init__.py, I changed the import code to from forms app import views.
from django.contrib import admin
from django.urls import path
import main.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', main.views.home_page, name="home_page"),
]
Well, i was also facing the same issue. I was continuously getting server errors but then I figured out it was a simple error.
go to the urls.py file
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = index), #the path for our index view
]
I just had to remove name = index and the server worked with 0 errors.
enter image description here

How to setup urls.py to work with app/urls.py and templates in src with Django

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

Categories