Django Page not found (404) but URLs are correct - python

I encountered such an error, I get 404 error when trying to open the URL http://127.0.0.1:8000/account/
# apps/account/urls.py
from django.urls import path
from django.contrib.auth.views import LoginView
from . import views
urlpatterns = [
path('', views.index),
path('login/', LoginView.as_view(template_name='account/login.html'), name="login")
]
And the strangest thing is that if I add something like
qwe/ in path
http://127.0.0.1:8000/account/qwe it will work, and 2nd path login/ is working
Can anyone tell me what the problem is?
#apps/account/views.py
from django.shortcuts import render, HttpResponse
def index(request):
return HttpResponse('<h1>Hello</h1>')
Main urls.py
#project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("apps.main.urls")),
path('account/', include("apps.account.urls")),
]
Screenshot

Not sure this will work but I suggest you try a different import in account views.py.
Rather than below.
#apps/account/views.py
from django.shortcuts import render, HttpResponse
Try below
#apps/account/views.py
from django.shortcuts import render
from django.http import HttpResponse

As per my knowledge you are making to pages as your main page.
urlpatterns = [
path('', views.index),
path('login/', LoginView.as_view(template_name='account/login.html'), name="login")
]
here change the first path as
path('index/',views.index),
then try. This might work, because in your code you are pointing two path as main page so whenever you are including
http://127.0.0.1:8000/account/qwe and path('qwe/', views.index)
the index page is pointing to the index page through path qwe.

Related

I still get You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs

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

How do I fix the circular import in django

I am trying to simply put a 'Hello World' text on the server. And it brings an error.
Project Urls.py :
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls'))
]
App Urls.py
from django.urls import path
from . import views
urlpattern = [
path('', views.index,name = 'index')
]
Views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(response):
return HttpResponse('<h1>Hello World</h1>')
The Error it tells me:
The included URLconf in myapp/urls.py 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.
You are missing an 's' in your apps urls.py.
it has to be 'urlpatterns', instead of 'urlpattern'
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.

Cannot open another HTML file from Django

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

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

Categories