I have configured every thing but django says page not found - django

can any one help me with this problem?
this is my blog/urls.py:
from django.urls import path
from .views import *
urlpatterns = [
path("/testTemplate", template_testing, name="testtemplate"),
]
and this is my website/urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and this is blog/views.py:
from django.shortcuts import render
# Create your views here.
def template_testing(request):
return render(request, "blog/index.html", {})
every things looks OK but when I want to go to this url: 127.0.0.1:8000/testTemplate I get a page not found error. What is going wrong here?
please help me!

First of all, make sure that your test server is running by going to the directory containing manage.py. Then make sure you have typed:
python manage.py runserver
Once you've done this, note that the trouble with your script is that the method "template_testing" is not referencing the correct render file. Change the following lines:
def template_testing(request):
return render(request, "blog/index.html", {})
To:
def template_testing(request):
return render(request, "testTemplate", {})

the template names you mentioned in blog/urls.py and in blog/views.py are different
both must contain same template name.

Related

how to correctly import urls.py from app?

This is probably pretty simple but I can't get my head around it. I'm learning Django, have v3.0.4 installed and can't get the URLs from an app to work correctly.
On the project urls.py I have the following:
Project\urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include
from AppTwo import views
urlpatterns = [
path('', views.index, name='index'),
path('', include('AppTwo.urls')),
path('admin/', admin.site.urls),
]
I've created an app named "AppTwo" and have the following urls.py and views.py in the app:
AppTwo\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('/help', views.help, name='help'),
]
AppTwo\views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<em>My Second App</em>")
def help(request):
return HttpResponse("<em>Help Page!!!</em>")
If I browse to http://127.0.0.1:8000/ the index page loads and I see the text "My Second App" as expected. However if I browse to http://127.0.0.1:8000/help I get page not found 404 error.
I can also browse to the admin page just fine. So far this is a stock project, the only other change I made after creating it was to the settings.py file to install the "AppTwo" application. Based on the documentation, this looks like it should work, so what am I doing wrong?
yep, knew it was simple.
Changed
path('/help', views.help, name='help'),
to:
path('help/', views.help, name='help'),
all good now.

Django error:view must be a callable or a list/tuple in the case of include()

Well i'm new to Django and i'm following outdated course (its free obv) and ran up to this error. What can i change in my code>
Here are codes from views.py and urls.py from both folders:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("This is teh index view!")
#next one
from django.urls import include, path
from msg import views
urlpatterns = path('', r'^$',views.index,name = "index")
#next one
from django.contrib import admin
from django.urls import path
from msg.urls import urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('msg/', urlpatterns),
]
after trying to makemigrations i get this error :
TypeError: view must be a callable or a list/tuple in the case of include().
It's because your urlpatterns path() syntax is wrong see
path(route, view, kwargs=None, name=None)ΒΆ
urlpatterns = [path('',views.index,name = "index")]
You are using a regular expression in your path, that has been deprecated so you should pick a tutorial that uses that format.
The reason you get the error when running migrations is that the project starts when you run Manage.py and then the app starts immediately afterwards. The app start does some basic checks and then borks if your have an error in the URLs file.

How to fix 'The included URLconf 'gp.urls' does not appear to have any patterns in it'

when i tried to write the first urlpattern and the first view, i got this error, so i can be able to access to the authentification template, i have no idea what can be the source of this error
# my gp/urls.py
from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html')
when i try to run the server this is the error i get
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'gp.urls' 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.
this is my program tree
gp
apps
conge
organisation
personnel
stage
gp
pycache
init.py
settings.py
urls.py
views.py
wsgi.py
static
templates
gp
index.html
db.sqlte3
manage.py
# my gp/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html', {})
what i edited is:
instead of from django.conf.urls import url i wrote from
django.urls import path
added {} in render function (its optional but just in case :) )
As stated in here: https://code.djangoproject.com/ticket/30728#no1
Maybe try to use latest Python version. Python 3.9 able to work

URLconf does not appear to have any patterns in it/Circular import error

I am following a tutorial found in the Django documentation and upon attempting to map a view to a URL I received the following error:
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'pollSite.urls' d
oes not appear to have any patterns in it. If you see valid patterns in the file th
en the issue is probably caused by a circular import
. I have a pollSite project and a poll app.
pollSite/pollSite/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
pollSite/poll:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world")
I thought I may have mistyped something so I went back and copied the code from the docs and pasted it directly into my editor and still got the same error. I'm not sure what a circular import is but I am also working with virtualenv for the first time and am not sure if that could be causing this. Any suggestions?
Tutorial incase anyone was interested: https://docs.djangoproject.com/en/2.2/intro/tutorial01/
Your app is called "poll", not "polls". So you have to include it by that name:
path('polls/', include('poll.urls')),

no module named search after adding project url

I've had the pleasure to work with somebody yesterday on the issue with my urls here Adding an additional template to an existing project errors out, but after trying everything suggested i'm still in the same situation.
My project is named mysite and my application is search.
It was suggested to add the following to my project urls.py
url(r'^search/', include('search.urls')),
When doing so I'm given the error ModuleNotFoundError: No module named 'search'.
My project urls.py is the following:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('search.urls')),
]
I'm attempting to add the following to my app urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
I have an empty view for results defined as
def results(request):
return render(request, 'results.html')
When I try to add the following POST to my form for the results it gives me the error in the first post. When I have the results url in my app.urls.py
<form action = "{% url 'results' %}" form method = "POST">
This is what my current application structure looks like. Please help get me on the right track. Thank you.
Your search directory is in your mysite directory (the one that includes settings.py. That means you should include mysite.search.urls (just as you use mysite.search in your import and INSTALLED_APPS).
from mysite.search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('mysite.search.urls')),
]
If your search directory was in your project directory (the one that includes manage.py, then you would remove mysite from the import, include() and INSTALLED_APPS.
from search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('search.urls')),
]