GET /css/styles.css HTTP/1.1" 404 3609 Python - python

Im developing a website on python and I found a theme that I like on the internet. It includes the index.html, css folder, js folder and the images. Like in other projects I copies all of the data into the templates folder in my project. I replaced my base.html with this new index.html. The problem is that Django can't find the css and js folders that are being references in the html. I have read some post about this, and everybody mentions taking the index.html outside of templates (doesn't work) or using a static folder to store my css and js files (doesn't work. Can someone give me some clues? thank you!!
Project
mysite
templates
assets
css
js
index.html
link href="{% static 'css/styles.css' %}" rel="stylesheet"/
from django.contrib import admin
from django.urls import path
from personal.views import home_screen_view
from account.views import registration_view, logout_view, login_view, account_view
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_screen_view, name="home"),
path('register/', registration_view, name="register"),
path('logout/', logout_view, name="logout"),
path('login/', login_view, name="login"),
path('account/', account_view, name="account"),
# Password reset links (ref: https://github.com/django/django/blob/master/django/contrib/auth/views.py)
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(template_name='registration/password_change_done.html'),
name='password_change_done'),
path('password_change/', auth_views.PasswordChangeView.as_view(template_name='registration/password_change.html'),
name='password_change'),
path('password_reset/done/', auth_views.PasswordResetCompleteView.as_view(template_name='registration/password_reset_done.html'),
name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(template_name='registration/password_reset_complete.html'),
name='password_reset_complete'),
]

Related

Django 4.0 shows path not found even after configuring all urls and views properly

So my django app looks like this
-assaydash
--dashboard (app)
--members (app)
--djangobackend (django project directory)
--static
--manage.py
djangobackend/settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dashboard.apps.DashboardConfig',
'members.apps.MembersConfig',
]
djangobackend/urls.py
urlpatterns = [
path('admin/', admin.site.urls, name='admin'),
path('', include('dashboard.urls')),
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
members/urls.py
urlpatterns = [
path('login_user', views.login_user, name="login"),
path('logout_user', views.logout_user, name='logout'),
]
members/views.py
def login_user(request):
if request.method == "POST":
...
else:
messages.success(request, ("There Was An Error Logging In, Try Again..."))
return redirect('login')
else:
return render(request, 'authenticate/login.html', {})
def logout_user(request):
logout(request)
messages.success(request, ("You Were Logged Out!"))
return redirect('home')
index.html
<a href="{% url 'login' %}" >
<span class="d-sm-inline d-none">Sign In</span>
</a>
On the index page when i click on the a tag it shows me a debug page with path not found. Even though the url exists and i have included it in my main urls.py and i have added the app in the settings.py as well. please help and let me know what is wrong here.
Notice that the url for name login was detected by the template but the path wasn't found. Also the other urls on the debug page are from my dashboard app which works fine
Because you have set the url path of django.contrib.auth.urls to members/ and the django.contrib.auth.urls include patterns of /members/login/ for name=login. Since urlpatterns will look from upper to lower your /members/login/ called instead of /members/login_userand look for corresponding page. So change your url path to others than members/ in
path('members/', include('django.contrib.auth.urls')),
and use members/ for include('members.urls').
If you have set
urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')),
]
The django.contrib.auth.urls include urlpatterns as
accounts/login/ [name='login']
accounts/logout/ [name='logout']
accounts/password_change/ [name='password_change']
accounts/password_change/done/ [name='password_change_done']
accounts/password_reset/ [name='password_reset']
accounts/password_reset/done/ [name='password_reset_done']
accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
accounts/reset/done/ [name='password_reset_complete']
See Authentication Views[Django-doc]
So change your djangobackend/urls.py as
urlpatterns = [
path('admin/', admin.site.urls, name='admin'),
path('', include('dashboard.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
Also change your member urls.py as
urlpatterns = [
path('login_user', views.login_user, name="login-user"),
path('logout_user', views.logout_user, name='logout-user'),
]
Also in html
<a href="{% url 'login-user' %}" >
<span class="d-sm-inline d-none">Sign In</span>
</a>

Django url pattern is not a registered view function or pattern

I have a small Django web app, with multiple applications inside them. I have used the include in the urls.py files, but whenever I reference the URLs in the HTML files they don't load. Below are my 3 urls.py files. The one I'm having an issue will specifically is the nodes url pattern in the nodes urls.py
#main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('account.urls')),
]
#account urls.py
from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views
app_name = 'account'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('dashboard/', views.dashboard, name='dashboard'),
path('nodes/', include('nodes.urls')),
]
#nodes urls.py
from django.urls import path
from . import views
app_name = 'nodes'
urlpatterns = [
path('', views.nodes, name='nodes'),
]
This is my HTML file where I am referencing the URL pattern:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'nodes' %}">Nodes</a>
</li>
Due to this pattern name you set in urls file if change HTML file to this it must be fixed:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'account:nodes:nodes' %}">Nodes</a>
</li>
just for tips :) if import urls file at the django shell and print list of urlspatterns you could see default name assign to your nodes path.

Django Open rendered HTML file

Guys i want to open rendered HTML file when i am clicking a image on my HTML page. How i should use it?
HTML code fragment
<header>
<nav class="headlist">
<ul>
<li>O nas</li>
<li>Kontakt</li>
<li><a>Zajęcia</a></li>
</ul>
</nav>
</header>
main app urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
pages app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('kontakt/', views.contact_view),
path('o_nas/', views.about_view),
path('', views.home_view),
]
pages app views.py
from django.shortcuts import render
def home_view(reqest):
return render(reqest, "index.html")
def about_view(reqest):
return render(reqest, "about.html")
def contact_view(reqest):
return render(reqest, "contact.html")
First things first: add a name to each route inside pages app urls.py.
pages app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('kontakt/', views.contact_view, name='contact'),
path('o_nas/', views.about_view, name='about'),
path('', views.home_view, name='home'),
]
Inside the HTML Template you can use the url tag in combination with the name of the route to assign the desired target to the href.
HTML code fragment
<header>
<nav class="headlist">
<ul>
<li>O nas</li>
<li>Kontakt</li>
<li><a>Zajęcia</a></li>
</ul>
</nav>
</header>

Django urlpatterns settings

I have a Django project, the working urls.py looks like:
urlpatterns = [
path('', views.index, name='index'),
path('polls/search', views.search, name='search'),
]
Then I want to add additional path for my image in the urls.py
urlpatterns += patterns('django.views.static',(r'^media/(?P<path>.*)','serve',{'document_root':settings.MEDIA_ROOT}), )
But I got:
unresolved reference 'patterns'
I am using python 3.4 and Django 2.0.8. How do I properly add the additional path to my original urls.py ? Thanks!
It looks like using patterns won't work anymore. Since you're trying to serve static files, try this:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And set MEDIA_URL and MEDIA_ROOT in your settings.py.
In order to get it to work in your templates, you'd do something like this:
{% load static %}
<body data-media-url="{% get_media_prefix %}">
Django docs

Writing my first Django app

I am trying my hand at learning Django and trying out the step-by-step tutorial at https://docs.djangoproject.com/en/2.0/intro/tutorial03/.
I have completed the app (well, till the Part 7) and is working as expected (and has been explained in the tutorial).
The only problem (so far) I am facing is when I am trying to navigate from the "Admin" page to the linked page "VIEW SITE" when I am being presented with "Page not found (404)" error. An image is being attached to make the situation clearer.
The link is pointing to "http://127.0.0.1:8000/" whereas it should be pointing to "http://127.0.0.1:8000/polls/". When I add the missing part of the path (manually) in the address bar the correct page (as expected) is presented.
I have tried to search on this as well as many other forums but could not get the right solution.
I am using Django 2.0.6 and Python 3.6.4 on mac sierra.
Shall be grateful for a lead on this.
Thanks
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
polls/template/polls/index.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Error on navigation at VIEW SITE
you should open http://127.0.0.1:8000/polls/
not
http://127.0.0.1:8000/.
If you wanna use http://127.0.0.1:8000/ then your path should be
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
It should be like this:
path('', include('polls.urls')),
not like this:
path('polls/', include('polls.urls'))
Because it should be the root url of your website
Here is what I have done (may be not the most elegant solution but works just fine).
I have modified the "mysite/urls.py" file as shown:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')),
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
This way I am able to access the "polls" page from both "View Site" link on the Django Admin page (url: "127.0.0.1:8000") as well as from link residing elsewhere (url: "127.0.0.1:8000/polls/").
Thanks for all the help.
PS. Visiting https://docs.djangoproject.com/en/2.0/topics/http/urls/ may be of help to learners like me.

Categories