How to fix: Page not found (404) in django - python

I'm new in Django. My problem is when I click the title which is in a.html, it's directing me to this URL; http://localhost:8000/aba/R%C3%BCyada%20Aba%20G%C3%B6rmek
in this URL I'm getting an error message; Page not found 404
Here is I wanna do; When I click the title, I wanna show my article details dynamically
dreamarticle/urls.py
from django.contrib import admin
from django.urls import path, re_path
from dreamarticle import views
app_name = "Dreamarticle"
urlpatterns = [
re_path('/aba/(.*?)/',views.aba,name="aba"),
path('a/',views.a,name="a"),
]
blog/urls.py
from django.contrib import admin
from django.urls import path,include
from article import views
from dreamarticle import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
path('articles/',include("article.urls")),
path('user/',include("user.urls")),
path('dreamarticle/',include("dreamarticle.urls")),
]
dreamarticle/views.py
def aba(request, title):
context = dict()
context.update(
dreamarticlesaba=get_object_or_404(dreamarticle, title=title),
)
return render(request, "aba.html", context)
aba.html
{% extends "layout.html" %}
{% block body%}
<!-- Content here -->
<br>
<div class="container">
<div class="row">
{% if dreamarticlesaba %}
{% for dreamarticle in dreamarticlesaba %}
<div class="col-md-12 dreamdetail">
<h1>{{dreamarticle.title}}</h1>
<hr>
<p>{{dreamarticle.content|safe|escape}}<p>
</div>
{% endfor %}
{% endif %}
</div>
</div>
a.html
{% extends "layout.html" %}
{% block body%}
{% include "includes/searchbar.html" %}
<!-- Content here -->
<br>
<div class="container">
<div class="row">
{% if dreamarticles %}
{% for dreamarticle in dreamarticles %}
<div class="col-md-4 text-center bg-primary">
<br><b>{{dreamarticle.title}}</b>
<b>{{dreamarticle.title}}</b>
</a>
</div>
{% endfor %}
{% endif %}
</div>
</div>
{% endblock body%}
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/aba/R%C3%BCyada%20Aba%20G%C3%B6rmek
Using the URLconf defined in blog.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
articles/
user/
dreamarticle/
The current path, aba/Rüyada Aba Görmek, 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.

Related

ProgrammingError at /blog/ django project in ubuntu vps

I encountered this error after deploying the project on a Linux server. But when running it on localhost, I had no error like this in addition that I say, I have a blog application
views.py
from django.views import generic
from .models import Post
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by("-created_at")
template_name = "blog/blog.html"
class PostDetail(generic.DetailView):
model = Post
template_name = "blog/blogContent.html"
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.PostList.as_view(), name="blog"),
path("<slug:slug>/", views.PostDetail.as_view(), name="post_detail"),
]
blog.html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<main class="main">
<div class="container blog_container">
<h2>وبلاگ</h2>
<div class="content_container">
{% for post in post_list %}
<div class="content">
<img src="{{ post.image.url }}" alt="" >
{{ post.title }}
<p>{{ post.author }} </p>
<p>{{ post.created_at|timesince }}</p>
<br>
<p class="description">{{post.content|slice:":200" }} </p>
</div>
{% endfor %}
</div>
</div>
</main>
<!-- START FOOTER COMPONENT -->
{% endblock content %}

Django User Login Issue

I am back again with a Django Question. I am having issues with getting my login screen to work. I feel like maybe this is a simple issue that I am just not seeing. I am using the current versions of Python and Django. I am getting a NoReverseMatch error when trying to access my login page. Here is the code, along with some screen shots:
base.html:
<p>
Learning Log -
Topics -
{% if user.is_authenticated %}
Hello, {{ user.username }}.
{% else %}
log in
{% endif %}
</p>
{% block content %}{% endblock content %}
login.html:
{% extends "learning_logs/base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'users:login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">log in</button>
<input type="hidden" name="next" value="{% url 'learning_logs:index' %}"/>
</form>
{% endblock content %}
users/urls.py:
from django.urls import path
from django.conf.urls import url
from django.contrib.auth.views import LoginView
from . import views
app_name = 'users'
urlpatterns = [
# Login page
#path('login/', LoginView, {'template_name': 'users/login.html'}, name='login'),
path('login/', LoginView.as_view(template_name='users/login.html')),
]
Code location
Error message
Since you commented out the name='login' part. Django can no longer find a path(…) [Django-doc] with that name hence the error.
You should add the name=… parameter:
# users/urls.py
from django.urls import path
from django.contrib.auth.views import LoginView
from . import views
app_name = 'users'
urlpatterns = [
# Login page
path(
'login/',
LoginView.as_view(template_name='users/login.html'),
name='login' # name of the path
),
]

When we extend an allauth view and create a new url based on that view is the old "/accounts/*" URLs of django allauth still required?

I extended the SignupView of django-allauth and created a url "/account-signup/" and also some minor changes in the template and I'm using the url name of my url. So, it keeps showing error that:
NoReverseMatch at /account-signup/
Reverse for 'account_login' not found. 'account_login' is not a valid view function or pattern name.
I've tried searching where the url name account_login is used in the template. Also, I've tried enabling the default URLs given by django allauth. It doesnt show error when the allauth URLs are included in the urls.py file.
/signup.html
{% extends "account/base.html" %}
{% load i18n %}
{% block head_title %}{% trans "Signup" %}{% endblock %}
{% block content %}
<h1>{% trans "Sign Up" %}</h1>
<p>{% blocktrans %}Already have an account? Then please sign in.{% endblocktrans %}</p>
<p>some content from sugat</p>
<form class="signup" id="signup_form" method="post" action="{% url 'my_app:custom_signup' %}">
{% csrf_token %}
{{ form.as_p }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<button type="submit">{% trans "Sign Up" %} »</button>
</form>
{% endblock %}
/my_app/urls.py
from django.conf.urls import url
from .views import *
app_name = "my_app"
urlpatterns = [
url(r'^account-signup/$', AccountSignUp.as_view(), name="account_signup"),
]
/myproject/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^accounts/', include('allauth.urls')),
url(r'', include("my_app.urls")),
]

Problem with ERR_TOO_MANY_REDIRECTS django 2.1

I started to create login module in django. Login module is ok but I have problem with logout. When i click Logout - we see "error -ERR_TOO_MANY_REDIRECTS"
Probably something in this file is incorect: account/urls.py
from django.conf.urls import url
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'account'
urlpatterns = [
path('', auth_views.LoginView.as_view(template_name='account/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='registration/logout.html'), name='logout'),
path('logout-then-login/', auth_views.logout_then_login, name='logout_then_login'),
path('dashboard/', views.dashboard, name='dashboard'),
base.html
<body>
<div id="header">
{% if request.user.is_authenticated %}
<ul class="menu">
<li {% if section == "dashboard" %} class="selected"{% endif %}>
Panel główny
</li>
<li {% if section == "images" %} class="selected"{% endif %}>
Obrazy
</li>
<li {% if section == "people" %} class="selected"{% endif %}>
Ludzie
</li>
</ul>
{% endif %}
<span class="user">
{% if request.user.is_authenticated %}
Witaj, {{ request.user.first_name }}
Wyloguj
{% else %}
Zaloguj
{% endif %}
</span>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
logout.html
{% extends "base.html" %}
{% block title %} Wylogowanie {% endblock %}
{% block content %}
<h1>Wylogowanie</h1>
<p>Zostales wylogowany. Mozesz
zalogowac sie ponownie</p>
{% endblock %}
settings.html
...
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_REDIRECT_URL = reverse_lazy('account:logout')
show error
You've set LOGOUT_REDIRECT_URL to point back to the LogoutView which will cause a redirect loop. The LOGOUT_REDIRECT_URL should point to a URL that the user will be redirected to after they've logged out using the LogoutView.
Setting LOGOUT_REDIRECT_URL will override any template that's been set. Since you've explicitly set a template for the LogoutView in your urls.py, you should remove LOGOUT_REDIRECT_URL from your settings which will allow the template to be rendered.

Error: 'login' not found. 'login' is not a valid view function or pattern name

This is the first time I am using Django login template and I ran into a problem. It keeps saying that login is not a valid view function but I define it in the urls.py in my users app:
Not sure what I am doing wrong.
main urls.py
urlpatterns = [
path('', include('blogging_logs.urls', namespace='blogging_logs')),
path('users/', include('users.urls', namespace='users')),
path('admin/', admin.site.urls),
]
app: blogging_logs: base.html
<p>
Home
Categories
{% if user.is_authenticated %}
Hello, {{ user.username }}.
{% else %}
login in
{% endif %}
</p>
{% block content %}{% endblock content %}
app: users: urls.py
from django.contrib import admin
from django.urls import re_path, path
from django.contrib.auth import authenticate, login
from django.contrib.auth import views as auth_views
app_name = 'users'
urlpatterns = [
# Login Page
path('login/', auth_views.LoginView.as_view(template_name='login.html')),
]
app:users: login.html
{% extends "blogging_logs/base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password din't match. Please try again.</p>
{% endif %}
<form class="" method="post" action="{% url 'users:login' %}" >
{% csrf_token %}
{{ form.as_p }}
<button name='submit'> Login in </button>
<input type="hidden" name="next" value="{% url 'blogging_logs:index' %}" />
</form>
{% endblock content %}
You are missing the name argument for the path function:
Change the following line
path('login/', auth_views.LoginView.as_view(template_name='login.html')),
to
path('login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'),

Categories