On clicking logout button the custom landing page is not landing,but the default django-admin page is landing!
hey! I am trying to learn django from the book django2 by example so i created a dashboard page which is having a logout button but on clicking the button it shows the django-admin landing page and not the one I created !
urls.py(main-project)
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('account.urls')),
]
urls.py(account-app)
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
# post views
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='log_out'),
path('', views.dashboard, name='dashboard'),
# change password urls
path('password_change/',auth_views.PasswordChangeView.as_view(),
name='password_change'),
path('password_change/done/',auth_views.PasswordChangeDoneView.as_view(),
name='password_change_done'),
]
It should show thisenter image description here but apparantely it's showing the django-admin default logout pageenter image description here
logged_out.html
{% extends "base.html" %}
{% block title %}Logged out{% endblock %}
{% block content %}
<h1>Logged out</h1>
<p>You have been successfully logged out. You can <a href="{% url
"login" %}">log-in again</a>.</p>
{% endblock %}
update- got the solution.
Nevermind! I got the solution, I just have to mention my application before the django.contrib.admin as both of the app and default admin shares the same urls and views. So in order to view mine custom pages I have to change the settings in the INSTALLED_APPS as per the above settings..
Related
I have a ran into a difficulty when navigating between different templates in a Django (v3.2) application. The app is called 'manage_remittance'.
The default landing page (which uses template manage_remittance/templates/manage_remittance/view_remittance.html) for the app should show a list of items (list is not relevant at the moment), and at the top of that list there should be a link, leading to another page in the same app, which would allow to add new items to the list.
The form that is invoked first is here:
manage_remittance/templates/manage_remittance/view_remittance.html
{% extends "root.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% url 'manage_remittance:remittance_add' as remittance_add %}
{% block title %}
VIEW REMITTANCES PAGE
{% endblock title %}
{% block content %}
<div class="list-group col-6">
Click here to add remittance data
</div>
I want to be able to get to another template (manage_remittance/templates/manage_remittance/remittance_add.html), but the {{ remittance_add }} has no value.
In addition, when I specify exact name of the html file (remittance_add.html) in the a href (see above), and click on it, I get a following error:
Using the URLconf defined in sanctions_project.urls, Django tried these URL patterns, in this order:
admin/
[name='login']
login/ [name='login']
logout/ [name='logout']
manage_remittance/ [name='view_remittance']
manage_remittance/ remittance_add/ [name='create_remittance']
^static/(?P<path>.*)$
^media/(?P<path>.*)$
The current path, manage_remittance/remittance_add.html, didn’t match any of these.
What am I doing wrong here?
fragment of urls.py for the project:
urlpatterns = [
path('admin/', admin.site.urls),
path('', login_view, name='login'),
path('login/', login_view, name='login'),
path('logout/', logout_view, name='logout'),
path('manage_remittance/', include('manage_remittance.urls')), # namespace='manage_remittance'
]
urls.py at manage_remittance app:
from .views import (
CreateRemittanceInfo,
RemittanceListView
)
app_name = 'manage_remittance'
urlpatterns = [
path('', RemittanceListView.as_view(), name='view_remittance'),
path('remittance_add/', CreateRemittanceInfo.as_view(), name='create_remittance'),
]
views.py at manage_remittance app:
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django.views.generic import ListView
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Remittance
class CreateRemittanceInfo(LoginRequiredMixin, CreateView):
model = Remittance
fields = ['remittance_text']
template_name_suffix = '_add'
class RemittanceListView(ListView):
model = Remittance
template_name = 'manage_remittance/view_remittance.html'
It seems that you have an issue in your urls:
manage_remittance/ remittance_add/ [name='create_remittance']
the name does not match with
manage_remittance:remittance_add
It seems quite simple: in your template, you should display a list, which is not the case. Have a look at Django User Guide: https://docs.djangoproject.com/fr/3.2/ref/class-based-views/generic-display/
It should look like
{% for object in object_list %}
<p>{{object.remittance_text}}</p>
{% endfor %}
I created this simple code to learn about login and registration in django. My views.py is
def index(request):
products = Product.objects.all()
return render(request,'index.html',{'products':products})
def register(request):
return render(request,'register.html')
def login(request):
return render(request,'login.html')
def logout(request):
return HttpResponse('logout')
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('register/', views.register),
path('login/', views.login),
path('logout/', views.logout),
]
base.html
Register
Login
<h3>Base html </h3>
{% block content %}
{% endblock %}
register.html
{%extends 'base.html'%}
<h1>Register</h1>
{% block content %}
{% endblock %}
login.html
{%extends 'base.html'%}
<h1>Login</h1>
{% block content %}
{% endblock %}
At http://127.0.0.1:8000/ when I click Register link it follows to http://127.0.0.1:8000/register page but when I click the 'Register link at that page it follows to http://127.0.0.1:8000/register/register which gives 404 error. How do I unfollow url that is instead of going to http://127.0.0.1:8000/register/register it has to go http://127.0.0.1:8000/register/?
in your base. html you should generally specify a link with {% url 'register' %}
So like that:
Register
But to use it, in you settings.py you should specify the directory where your templates are stored relative to the base directory of your project
TEMPLATES = [
{
...
'DIRS': [
os.path.join(BASE_DIR, 'your/way/to/templates'),
],
...
]
Also import os.path at the top of settings.py
EDIT
when you specify your urls.py, you should give name to every url.
path('register/', views.register, name='register'),
so in Register you call the name of the url
Haven't seen your base urls from project side but seems your are doing this way in my guess
you can remove register from base urls of project file
path(
'register/',
include('register.urls'),
),
The best practice for this problem to name the URLs. Just give them names and refer to that names in the HTML file
My solution:
urls.py
urlpatterns = [
path('', views.index),
path('register/', views.register,name="register"),
path('login/', views.login,name="login"),
path('logout/', views.logout,name="logout"),
]
base.html
Register
Login
<h3>Base html </h3>
I'm learning Chapter 18 18.4.2 in Python Crash Course,when i open http://localhost:8000/topics ,I'm using Django 3.0 and python 3.8
shows
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/topics
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, topics, 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.
and this is my code
learning_log\urls.py
from django.contrib import admin
from django.urls import path
from learning_logs import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name = 'index')
]
learning_logs\urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name = 'index'),
url(r'^topics/$',views.topics,name='topics')
]
views.py
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
return render(request,'learning_logs/index.html')
def topics(request):
topics = Topic.objects.order_by('date_added')
context = {'topics':topics}
return render(request,'learning_logs/topics.html',context)
base.html
<p>
Learning Log-
Topics
</p>
{% block content %}{% endblock content %}
topics.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topics</p>
<ul>
{% for topic in topics %}
<li>{{ topic }}</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
and runserver shows:
Not Found: /topics
[06/Jan/2020 17:53:15] "GET /topics HTTP/1.1" 404 2077
enter image description here
First of all, good question. Love the detail.
The error is the following
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order
This means that the url /topics cannot be found. You need to import that url from the app specific urls.py into the main urls.py. This is usually done by something along the lines of
# in main urls.py
from learning_logs.urls import urlpatterns as ll_urlpatterns
# other patterns ...
urlpatterns += ll_urlpatterns
Use below code replace 'app' with your appname.
from django.contrib import admin
from django.urls import path
from learning_logs import views
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include(('app.urls', 'app'), namespace='app')),
]
http://127.0.0.1:8000/accounts/login/
This page is created successfully and working nice:
And Problem with these pages: http://127.0.0.1:8000/accounts/logout/ This page redirect me Django Administration Pages that message to login again but I don't want this.
I have placed my HTML file in
..templates/registration/logged_out.html
And checked spelling error multiple time to see why not working.
My urls files:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('catalog.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and my logged_out.html is:
{% extends 'base.html' %}
{% block content %}
<p>Logged out!</p>
Click here to login again.
{% endblock %}
Can any tell me what's wrong with it? How to fix this?
If you have defined LOGOUT_REDIRECT_URL in your settings, then LogoutView will redirect to it.
Check to see if you have defined LOGOUT_REDIRECT_URL in your settings, and remove it if you have.
So I have been trying to access a project post that I have created for my website, but every time I click on it the url can't be found. I am not sure why.
My code is below:
mywebsite/urls.py
from django.contrib import admin
from django.urls import re_path, include
from django.conf import settings
from django.conf.urls.static import static
#re_path(r'^admin/', admin.site.urls),
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('home.urls')),
re_path(r'^projects/', include('projects.urls', namespace="create_post")),
re_path(r'^contact/', include('contact.urls')),
]
projects/urls.py
from django.urls import re_path, include
from . import views
# urls for projects page
app_name = 'create_post'
urlpatterns = [
re_path(r'^$', views.retrieve_projects, name="retrieve_projects"),
#re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^(?P<slug>[\w-]+)/$', views.details_of_project, name="details_of_project"),
re_path(r'^(?P<slug>[\w-]+)/update/$', views.update_projects, name="update_projects"),
re_path(r'^(?P<slug>[\w-]+)/delete/$', views.delete_projects, name="delete_projects"),
]
# To make images work
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
projects/views.py
# Function to retrieve all different projects
def retrieve_projects(request):
# Retrieves objects based on latest publish date
projects_list = Projects.objects.all().order_by("-publish_date")
context = {
'projects_list': projects_list,
}
return render(request, 'projects/projects.html', context)
projects/projects.html
<h1>asdasdasdasdasdasdas</h1>
{% if projects_list %}
<ul>
{% for project in projects_list %}
<h2>{{project.title}}</h2>
<h2>{{project.description}}</h2>
<h2>{{ project.publish_date }}</h2>
{% endfor %}
</ul>
{% endif %}
The error returns page not found
try this