I'm trying to create a dashboard on Django
It consists of 7 pages and every third page is giving error
PYCHARM, django module
Localhost
Python 3,7
Django Latest version
I have written all the URLs properly And it comes like that
Localhost/maps
this one is fine but the next click is
Localhost/maps/reports
now this is error
I want that every time a link is clicked it goes back to localhost and then take the new link.The codes urls are like this
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('login/', views.login),
path('maps/', views.maps),
path('profile/', views.profile),
path('preferences/', views.preferences),
path('register/', views.register),
path('reports/', views.reports),
path('weather/', views.weather),
path('help/', views.help),
]
Related
The problem appeared after I added a template to a minimal webapp. The template extends django-oscar's layout.html. Nothing else in the project extends layout.html.
My goal is simply to be able to use django-oscar templates to form the basis of web pages in my webapp. For some reason I am having no end of issues. This is only the latest error message. I have been struggling with this for days! When I resolve one issue, another shows up.
I made a minimal git repo for this problem: https://github.com/mslinn/django_oscar_problem
The repo has a requirements.txt file in case anyone wants to install the PIP modules necessary run the program.
I tried to ensure that I had the simplest possible project that shows the problem. In README.md I show the complete error message displayed in the web browser.
# /templates/welcome.html
{% extends 'oscar/layout.html' %}
... etc ...
# main/urls.py
from django.urls import include, path
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, 'home'),
path('admin/', admin.site.urls, 'admin'),
path('hello/', views.hello, 'hello'),
path('', include(apps.get_app_config('oscar').urls[0])),
]
# main/views.py
from django.shortcuts import render
def home(request):
return render(request, "welcome.html", {})
I then ran the webapp with:
$ ./manage.py runserver
The error message in the web browser after visiting http://localhost:8000 is:
ValueError at /
dictionary update sequence element #0 has length 1; 2 is required
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 3.1.6
Exception Type: ValueError
Exception Value:
dictionary update sequence element #0 has length 1; 2 is required
The problem is here:
urlpatterns = [
path('', views.home, 'home'),
path('admin/', admin.site.urls, 'admin'),
path('hello/', views.hello, 'hello'),
path('', include(apps.get_app_config('oscar').urls[0])),
]
The arguments you're passing to path() are not quite right. The signature for path is:
path(route, view, kwargs=None, name=None)
i.e., the third positional argument is expected to be kwargs passed to the view function - but you've passed a string instead, which is what causes the somewhat obscure error. I think you intend this to be the name, in which case you need to supply that as a named argument, i.e.,:
urlpatterns = [
path('', views.home, name='home'),
path('admin/', admin.site.urls, name='admin'),
path('hello/', views.hello, name='hello'),
path('', include(apps.get_app_config('oscar').urls[0])),
]
Note that this then results in a new TemplateDoesNotExist error in your sample repo - this is because the location of the templates directory is not somewhere that Django knows to look. You can fix this by adding the path to that directory to the DIRS configuration in your template settings, which is currently set to [].
I'm following Mosh's tutorial video on Python. He begins the django section (https://youtu.be/_uQrJ0TkZlc?t=18085) by installing django 2.1. I am able to open a development server the first time as he does:
pip install django==2.1
django-admin startproject pyshop .
python manage.py runserver #server works
Here are the steps he goes through to add a "products" app/path:
python manage.py startapp products
Opens views.py from this new products folder and modifies code to this:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse('Hello World')
Creates urls.py inside the products app/folder and adds this code:
from django.urls import path
from . import views
urlpatterns = [
path(' ', views.index) # Here should have been '' and now fixed
]
Opens the main urls.py in the pyshop folder and adds/modifies the end of the file like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
Mosh goes back to the server and adds /python to the url to get a page with "Hello World"
(https://youtu.be/_uQrJ0TkZlc?t=19220)
Upon trying to run the server again, I get Page not found error. Is there something I'm missing? I didn't figure it'd be a version issue since I made sure and installed the same 2.1 version.
As Jeffery and Daniel mentioned, I had an incorrect space and it needed to be an empty string in the products urls.py path. However, I'm still getting page not found error. Here's the exact error message if that helps:
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
The empty path didn't match any of these.
Modify your url path in the products app/folder like this
urlpatterns = [
path('', views.index)]
Remove the space in between
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('love/', include('love.urls')),
path('admin/', admin.site.urls),
]
I tried this code by seeing n online documentation and I see the code as same as the one in the documentation but I see the error and I can not start practicing django.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('love/', include('love.urls')),
path('admin/', admin.site.urls),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in ad.urls, Django tried these URL patterns, in this order:
^love/
^admin/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
It is being shown on the web
you must define empty path to use 127.0.0.1:8000
path('', include('love.urls')),
When going to login to the Django admin on my page, I geta 404 error and a "/admin/login/ url is not defined".
I only get this error while in the Production of my project - it works just fine locally. I am using A2 hosting and their support team has not been able to help me solve this problem.
The stack trace as well as the error url are seen in the second image.
Let me know if you need to see any code, I am more than happy to share I just dont want to be here all day posing all of my .py files when most of them wont matter anyways.
Code by request:
urls.py
from django.contrib import admin
from django.conf.urls import url, include
from django.views.generic.base import TemplateView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^students/', include("students.urls")),
url(r'^$', TemplateView.as_view(template_name="home.html"), name="home"),
url(r'^home/$', TemplateView.as_view(template_name="home.html"), name="home"),
url(r'^about/$', TemplateView.as_view(template_name="about.html"), name="about"),
url(r'^how_to_sponsor/$', TemplateView.as_view(template_name="how_to_sponsor.html"), name="how_to_sponsor"),
url(r'^malawi/$', TemplateView.as_view(template_name="malawi.html"), name="malawi"),
url(r'^stories/$', TemplateView.as_view(template_name="stories.html"), name="stories"),
url(r'^donations/$', TemplateView.as_view(template_name="donations.html"), name="donations"),
url(r'^staff/$', TemplateView.as_view(template_name="staff.html"), name="staff"),
url(r'^malawi_education/$', TemplateView.as_view(template_name="malawi_education.html"), name="malawi_education"),
]
admin.site.site_header = 'Maphunziro Project'
UPDATE:
I ran a migration and now the login screen displays like it normally does - I am still getting the login issue however.
Could this be a dependency problem? I have all of the same dependencies installed on the server as my localhost version but maybe I'm missing one that is required for production.
Try the address without the trailing slash 'www.educate-malawi.com/admin/login'. You can take a look at the documentation regarding the APPEND_SLASH here.
I'm a full beginner on Django. Therefore, I'm sorry if my question is not making so much sense.
I'm studying Django tutorial - step 1. I installed properly Django and created my first Django project named 'vanilla'. The urls.py script of the vanilla project is
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
When I start Django with this 'vanilla' project and start the development server at http://127.0.0.1:8000/, I correctly see the start page (with a rocket).
In a second project that I named 'djtutorial', I created as requested in Django tutorial - step 1 a 'polls' app. As requested, I modified urls.py file in djtutorial\polls which now has following content:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
If I start the server the url http://127.0.0.1:8000/polls/ is working properly. However, if I launch the root url of the site (i.e. http://127.0.0.1:8000/), I now have a 404 error.
Why is the 'root url' of the site now hidden?
There wasn't any root URL at all. Main page informing you that your project is created properly shows up only if you don't have any view of your own added to urlpatterns. When you create any page, this welcome page stops showing up and that is expected.
If you want to show your own page on root of your website, use this in your urlpatterns:
path('', >>VIEW OR INCLUDE HERE<<),