I just made the app users to validate user that is already registered in database. Included url inside project directory(urls.py), executed the login page in urls.py from app directory, made the template and a link refer in base.html. It all works, however when click Login link return this error:
TemplateDoesNotExist at users/login/
I tried to rename the path according tree navigation but always return this same error. Any idea what is happening?
Sorry my english
tree navigation in my project like this:
my_project
urls.py(project):
from django.contrib import admin
from django.urls import include, path
app_name = ['app_web_gym', 'users']
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls', namespace='users')),
path('', include('app_web_gym.urls', namespace='app_web_gym')),
]
urls.py(app)
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns= [
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]
base.html:
<p>
Web Gym-
Clientes-
Treinos-
Instrutores-
{% if user.is_authenticated %}
<p>Hello, {{user.username}}.<p/>
{% else %}
Login
{% endif %}
</p>
{% block content %} {% endblock content %}
login.html:
{% extends 'app_web_gym/base.html' %}
{% block content %}
{% if form.errors %}
<p>Wrong username/password. 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 'app_web_gym:index' %}" />
</form>
{% endblock content %}
I printed the full error:
TemplateDoesNotExist
I noticed that says Django tried loading these templates, in this order, in last line ->
/home/at_admin/prj01/app_web_gym/templates/users/login.html (Source does not exist)
thats the wrong path to login.html the correct is
/home/at_admin/prj01/users/templates/users/login.html as shown in tree navigation.
I don't know why is this happening and don't know how to fix it.
have you registered your app in main settings.py? It occurs sometimes when you forget to register your app
In main settings.py file:
at the end of, add one more line
INSTALLED_APPS = ['appname'] (replace appname with the name of your app having login page)
Hey people i just deleted all the app users and recreated it with the same coding that i previously copied. It worked this time though still don't know what happened.
Thanks all
First you should've checked if the app was installed in your settings. And as it looks in the error the files were being looked at the wrong place(app_web_gym/users/login.html) instead of (users/login.html). So I suppose you made a mistake in installing your app in settings.py.
Related
I have the following block of code, And I want to include a url to the login page, after the user logs out, in a translatable text.
Unfortunately, translation blocks cannot include tags, and I get the following error:
SyntaxError: Translation blocks must not include other block tags: url "account:login"
{% blocktrans %}
You have been successfully logged out.
You can log-in again.
{% endblocktrans %}
urls.py:
from django.urls import path
from django.contrib.auth import views as auth_views
app_name = 'account'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]
What would be the right way to achieve something like this?
Edit: I figured there are workarounds, such as translating blocks of text separately, or using javascript to append the "href" element after the page loads. But I wonder if there is a more efficient, Django way.
As documented
Reverse URL lookups cannot be carried out within the blocktrans and
should be retrieved (and stored) beforehand:
{% url 'path.to.view' arg arg2 as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}
This is my first attempt at utilizing Django and I've run into an issue with the URL & Views files. I created a project called "reading". Within that project, I created two apps, "products" and "speech". Within the "speech" urls.py file, I have the following code:
from django.urls import path
from speech.views import todoView, addTodo
urlpatterns = [
path('todo/', todoView),
path('addTodo/', addTodo)
]
In the views.py file I have the following code:
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from .models import TodoItem
def todoView(request):
all_todo_items = TodoItem.objects.all()
return render(request, 'todo.html',
{'all_items': all_todo_items})
def addTodo(request):
new_item = TodoItem(content = request.POST['content'])
new_item.save()
return HttpResponseRedirect('/todo/')
In the project folder, within the urls.py file, I have:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls')),
path('speech/', include('speech.urls'))
In the project folder, within the "templates" directory, I have a "todo.html" file with:
<h1>This is the todo page</h1>
<ul>
{% for todo_item in all_items %}
<li>{{ todo_item.content}}</li>
{% endfor %}
</ul>
<form action="/addTodo/" method="POST">{% csrf_token %}
<input type="text" name="content" />
<input type="submit" value="Add" />
</form>
When I attempt to add an item to the todo model, I get this error "The current path, addTodo/, didn't match any of these". I tried prepending "speech" to the form such as:
<form action="speech/addTodo/" method="POST">{% csrf_token %}
however, I now get this error:
The current path, speech/todo/speech/addTodo/, didn't match any of these.
Why is it duplicating the path when I prepend "speech" to the /addTodo action?
Not sure if this is associated to the error, but before I implemented a project level "templates" directory, I gave each app its own "templates" directory. Within those directories, I created an "index.html" file with separate content. When I had an index path for each apps, I couldn't get the apps to render the "index.html" file that was associated to it. Instead, it seemed the second application tried to render the "index.html" file of the first app. Is there a top down rule when it comes to templates in Django?
You missed a slash
Try this:
<form action="/speech/addTodo/" method="POST">{% csrf_token %}
A more 'Django' way of doing this could be:
from django.urls import path
from speech.views import todoView, addTodo
urlpatterns = [
path('todo/', todoView),
path('addTodo/', addTodo, name='add-todo')
]
Then in the template:
<form action="{% url 'add-todo' %}" method="POST">{% csrf_token %}
[solved]
i am trying to make a blog in django and on the github here is the code
i am trying set the html crispy form template in change password option and made some file names password_change_form.html, password_change_done.html etc. but when try to browse http://127.0.0.1:8000/accounts/password_change/done/ or any kind of pages related to password change section it is not showing the crispy form. but login or signup links are showing in crispy form. password change forms are showing in basic django form.
i want to change it into the desired ones.
i made two apps : blogapp and accounts. i am copying the urls below:
blogapp/urls.py:
from django.urls import path
from .views import (BlogappListView,
BlogappPostView,
BlogappCreateview,
BlogappUpdateView,
BlogappDeleteView,
)
urlpatterns = [
path('post/<int:pk>/delete/',BlogappDeleteView.as_view(),name='post_delete'),
path('post/<int:pk>/edit/',BlogappUpdateView.as_view(),name='post_edit'),
path('post/new/', BlogappCreateview.as_view(),name='post_new'),
path('post/<int:pk>/',BlogappPostView.as_view(),name='post_detail'),
path('',BlogappListView.as_view(),name='home'),
]
accounts/urls.py:
from django.urls import path
from .views import SignUpView
urlpatterns = [
path('signup/',SignUpView.as_view(),name='signup')
]
blog_project/urls.py:
from django.contrib import admin
from django.urls import path,include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/',include('accounts.urls')),
path('accounts/',include('django.contrib.auth.urls')),
path ('',include('blogapp.urls')),
path('',TemplateView.as_view(template_name='home.html'),name='home')
]
i pur password change forms htmls files under templates/registration folder. here is onehtml file for example:
{% extends 'base.html' %}
{% block title %}Forgot Your Password?{% endblock title %}
{% load crispy_forms_tags %}
{% block content %}
<h1>Forgot your password?</h1>
<p>Enter your email address below, and we'll email instructions for setting
a new one.</p>
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-success" type="submit" value="Send me instructions!">
</form>
{% endblock content %}
this file is named as password_reset_form.html. there are are couple of othe files named password_change_done.html, password_reset_complete.html etc none of the password related files html are not showing ....all the urls are showing the basic django template.
i just can't figure out what am i missing and or what did i wrong? password change suppose to show the html form....not in django basic form.
please let me know where is my mistake.thanx in advance
I solved it. I changed templates directory from blogapp to project folder directory where manage.py exists. And everything else I did right. And it took a lot of time to figure out which is silly. Thanx any way... kept the answer if someone need this
I can use the Django-Registration redux to login and register and it works fine except for the password change which forwards to: http://127.0.0.1:8000/?next=/accounts/password/change/ which lands on the homepage.
These are my app urls:
class MyRegistrationView(RegistrationView):
def get_success_url(self,user):
return('/createprofile/')
urlpatterns=[
url(r'^$', views.index, name='index'),
url(r'^city/(?P<city_name_slug>[\w\-]+)/$', views.show_city, name='show_city'),
url(r'^user/(?P<username>[\w\-]+)/$', views.show_profile, name='show_profile'),
url(r'^search/$', views.search, name='search'),
url(r'accounts/register/$', MyRegistrationView.as_view(), name='registraion_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^createprofile/$', views.createprofile, name="createprofile"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is the link the I use to change password:
<p>
forgot your password? Ney bother.
Reset password
</p>
This is the password_change_form.html form stored in the same place as other registration forms:
{% extends 'base.html' %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
This is the result from http://127.0.0.1:8000/accounts/:
EDIT: the above scenario happens when the user is not logged in. When the is logged in this error comes up:
NoReverseMatch at /accounts/password/change/ Reverse for
'auth_password_change_done' with arguments '()' and keyword arguments
'{}' not found. 0 pattern(s) tried: []
The issue turned out that I put this:
url(r'^accounts/', include('registration.backends.default.urls')),
in the app's url.py however, it needed to be in the project's url.py.
Background
I am trying to customize the authentication views in a Django project, but I can't seem to get the customized password_change view to run. I use Django 1.8.2 and Python 2.7.
The urls.py of my module userauth looks like the following:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('django.contrib.auth.views',
url(r'^login/$', 'login', {'template_name': 'userauth/login.html'},
name='userauth_login'),
url(r'^logout/$', 'logout', {'next_page': '/'},
name='userauth_logout'),
url(r'^password-change/$', 'password_change',
{'template_name': 'userauth/password_change_form.html'},
name='userauth_password_change'),
url(r'^password-change-done/$', 'password_change_done',
{'template_name': 'userauth/password_change_done.html'},
name='userauth_password_change_done'),
)
this is referenced in the main urls.py as this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('userauth.urls')),
]
The template of my userauth/password_change_form.html
{% extends "base.html" %}
{% block title %}{{ block.super }} - Change Password{% endblock %}
{% block toggle_login %}{% endblock %}
{% block content %}
<form action="{% url 'userauth_password_change' %}" method="post" accept-charset="utf-8">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" value="Change password"/>
</form>
{% endblock %}
And the template for userauth/password_change_done.html
{% extends "base.html" %}
{% block title %}{{ block.super }} - Password change successful{% endblock %}
{% block content %}
<p>Your password has been changed successfully.</p>
Back to your Account
{% endblock %}
The Problem
When I open the 'password_change_done' page (at /account/password-change-done), then everything is fine.
But at 'password-change' (/accunt/password-change) I am getting this error:
NoReverseMatch at /account/password-change/
Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
What I tried
I have no idea, why this should be happening.
I tried removing the single quotes from url 'userauth_password_change'
I made sure the password-change-donepage exists in urls.py and is available
I read the solutions at Reverse for '*' with arguments '()' and keyword arguments '{}' not found, Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found, Django change_password NoReverseMatch at /accounts/password/change/ (and a few more, I tried all the solutions there, but I can't find a problem in my own code)
Any help is appreciated. Thank you!
Ok, so the suggested solution for me didn't work here. I'm using Django 1.8.8 in an application with a specific app label, so I need to specify a url in a template like this e.g. app_label:url_name. This meant the reverse for password_change_done would never work since it's app_label:password_change_done.
But thankfully there's a solution: 'post_change_redirect'. Hence I specified password_change like this:
url(r'^password_change$', 'django.contrib.auth.views.password_change', {'template_name': 'password_change.html', 'post_change_redirect': 'app_label:password_change_done'}, name='password_change'),
I'm sure others could use this to overcome the problem above and still keep their own custom url name.
In some section you call the url named "password_change_done"
the correct name is: "userauth_password_change_done"
The solution was, that the in the urls.py the name of the password_change_done link must be 'password_change_done':
url(r'^password-change-done/$', 'password_change_done',
{'template_name': 'userauth/password_change_done.html'},
name='password_change_done'),
I had a look into django.contrib.auth.views.password_change (which was creating the problem) and realized, that the the url 'password_change_done' is hardcoded there in Django 1.8.2.
You need delete single quotes around the view name
{% url password_change_done %}
instead of
{% url 'password_change_done' %}
I meet the same kind of problem. The reason is the "password_change_done" is hard code in the auth.views, so I extense the class base on the auth.views.PasswordChangeView。
the auth.views.PasswordChangeView:
class PasswordChangeView(PasswordContextMixin, FormView):
......
success_url = reverse_lazy('password_change_done')
the MyPasswordChangeView:
class MyPasswordChangeView(PasswordChangeView):
success_url = reverse_lazy('app_label:password_change_done')
just overide the success_url.
and use MyPasswordChangeView in the urlpattern. Now, everything is ok.
in case if you are using
app_name
and trying to overwrite default template for update/change password you have to tell that Django:
from django.urls import path, reverse_lazy
from django.contrib.auth import views as auth_view
from . import views
app_name = 'account'
urlpatterns = [
path('login/', auth_view.LoginView.as_view(), name='login'),
path('logout/', auth_view.LogoutView.as_view(), name='logout'),
path('password_change/',
auth_view.PasswordChangeView.as_view(
template_name='registration/password_change_form.html',
success_url=reverse_lazy('account:password_change_done')), name='password_change'),
path('password_change/done/',
auth_view.PasswordChangeDoneView.as_view(
template_name='registration/password_change_done.html'), name='password_change_done'),
]