cant change language using {% url 'set_language' %} in Django translation - django

this was actually working fine i don't know what changed or happened.
suddenly {{ redirect_to }} as shown here from django documentation
<input name="next" type="hidden" value="{{ redirect_to }}">
doesn't have a value when i view it from developer tools
this my template code
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<li class="mx-0 mx-lg-1">
<form action="{% url 'set_language' %}" method="post" id="lang_form" style="display: flex; margin-top: 1rem;">{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}">
<select id="language_selection" onchange="ChangeLanguage(this);" class="form-control bg-primary text-white" name="language">
{% for language in languages %}
<option class="text-white" value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
{{ language.name_local }}
</option>
{% endfor %}
</select>
</form>
</li>
the form is submitted using this java script code
function ChangeLanguage(element){
const lang_form = document.getElementById('lang_form');
console.log(lang_form);
lang_form.submit();
}
and this is my main urls.py
rom django.contrib import admin
from django.urls import path, include
from django.conf import settings #add this
from django.conf.urls.static import static #add this
from django.contrib.auth import views as auth_views
from django.conf.urls.i18n import i18n_patterns
from django.views.i18n import JavaScriptCatalog
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('lazord.urls', namespace='lazord')),
path('authentication/', include('authentication.urls', namespace='authentication')),
path('booking/', include('booking.urls', namespace='booking')),
path('doctors/', include('doctors.urls', namespace='doctors')),
path('notifications/', include('notifications.urls', namespace='notifications')),
path('reception/', include('reception.urls', namespace='reception')),
path('instagram_api/', include('instagram_api.urls', namespace='instagram_api')),
path('i18n/', include('django.conf.urls.i18n')),
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
# reset password views
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='authentication/password_reset.html',
), name='reset_password'),
path('password_reset_done', auth_views.PasswordResetDoneView.as_view(
template_name='authentication/password_reset_sent.html'
), name='password_reset_done'),
path('password_reset_confirm/<uidb64>/<token>', auth_views.PasswordResetConfirmView.as_view(
template_name = 'authentication/password_reset_form.html'
), name='password_reset_confirm'),
path('password_reset_complete', auth_views.PasswordResetCompleteView.as_view(
template_name='authentication/password_reset_done.html'
), name='password_reset_complete'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns = i18n_patterns(
path('admin/', admin.site.urls),
path('', include('lazord.urls', namespace='lazord')),
path('authentication/', include('authentication.urls', namespace='authentication')),
path('booking/', include('booking.urls', namespace='booking')),
path('doctors/', include('doctors.urls', namespace='doctors')),
path('notifications/', include('notifications.urls', namespace='notifications')),
path('reception/', include('reception.urls', namespace='reception')),
path('instagram_api/', include('instagram_api.urls', namespace='instagram_api')),
path('i18n/', include('django.conf.urls.i18n')),
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
# reset password views
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='authentication/password_reset.html',
), name='reset_password'),
path('password_reset_done', auth_views.PasswordResetDoneView.as_view(
template_name='authentication/password_reset_sent.html'
), name='password_reset_done'),
path('password_reset_confirm/<uidb64>/<token>', auth_views.PasswordResetConfirmView.as_view(
template_name = 'authentication/password_reset_form.html'
), name='password_reset_confirm'),
path('password_reset_complete', auth_views.PasswordResetCompleteView.as_view(
template_name='authentication/password_reset_done.html'
), name='password_reset_complete'),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
i am currently changing the language by changing the language code in the link using js ( i don't know if this is right or not) i dont know what is messing or if changed something without me noticing thanks in advance for any help.

Related

Django form not visible after redirect

I'm trying to make django form to appear after user is logged in and authenticated but the form just does not appear on the page here is my code. I'm stuck on this for few days now and any help would be huge help
Views.py form code
def prasymas(request):
context = initialize_context(request)
user = context['user']
form = Prasymas(request.POST or None)
if form.is_valid():
form.save()
context ={
'form':form
}
return HttpResponseRedirect(reverse('home'))
urls.py code
from django.urls import path
from . import views
urlpatterns = [
# /
path('', views.home, name='home'),
# TEMPORARY
path('signin', views.sign_in, name='signin'),
path('signout', views.sign_out, name='signout'),
path('callback', views.callback, name='callback'),
path('prasymas', views.home, name='prasymas'),
]
template.py code
{% extends "loginas/layout.html" %}
{% block content %}
<div class="container">
<h1 class="d-flex justify-content-center"></h1>
<p class="d-flex justify-content-center"></p>
{% if user.is_authenticated %}
<h4>Sveiki {{ user.firstname }} {{user.surname}}
</h4>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-secondary">POST</button>
</form>
{% else %}<div class="d-flex justify-content-center">
Prisijungti
</div>
{% endif %}
</div>
{% endblock %}
From your urls.py it looks like the prasymas function in views.py is not being called.
Try making the following change to your urls.py file to include a call to the prasymas function.
Note: the line path('prasymas', views.prasymas, name='prasymas'), now points to views.prasymas not home. This means the path /prasymas will now render your form.
from django.urls import path
from . import views
urlpatterns = [
# /
path('', views.home, name='home'),
# TEMPORARY
path('signin', views.sign_in, name='signin'),
path('signout', views.sign_out, name='signout'),
path('callback', views.callback, name='callback'),
path('prasymas', views.prasymas, name='prasymas'),
]

I am having trouble rendering the fields in in-built password reset confirm form

When I use the in-built PasswordResetConfirmView the fields i.e. new password and password confirmation do not show up. I get a blank form with only the submit button.
One of the previous posts mentioned that one could try removing crispy from the corresponding template, which I tried without success.
I also note that the password reset link thatI received in email looks like this: http://127.0.0.1:8000/password-reset-confirm/MQ55a-1dead984b940e457bcad. Note that it is not .../MQ/ as I see in the examples. I tried inserting the / in the url but that did not load the template.
My url patterns...
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name = 'user/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name = 'user/logout.html'), name='logout'),
path('password-reset/', auth_views.PasswordResetView.as_view(
template_name = 'user/password_reset.html'),
name='password_reset'),
path('password-reset-done/', auth_views.PasswordResetDoneView.as_view(
template_name = 'user/password_reset_done.html'),
name='password_reset_done'),
path('password-reset-confirm/<uidb64><token>', auth_views.PasswordResetConfirmView.as_view(
template_name = 'user/password_reset_confirm.html'),
name='password_reset_confirm'),
path('', include('blog.urls')),
]
My password-reset-confirm.html is
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% crispy form %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Enter New Password</legend>
{{form|crispy}}
</fieldset>
<div class="form-group">
<button type="submit" class="btn btn-secondary">Change Password</button>
</div>
</form>
</div>
{% endblock content %}
I expect to see the password and password confirm fields. Instead I get
the screen which is at this link.
https://drive.google.com/open?id=1E61fjoy5K26nA0Ooej56gRu0DPLFzfXo
I found the problem in my url_patterns:
I had missed out the / between uidb64 and token parameters for the path password-reset-confirm

Django translate refers to wrong URL

I am using Django (version 2.1.3) in one of my project and currently struggle with a weird bug. I use the build in internationalization module and included a language toggler in my main menu which is loaded on every page
{% get_current_language as LANGUAGE_CODE %}
<form id="form" action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ request.get_full_path|strip_lang }}" />
<input id="form_lang" name="language" type="hidden" value="{{ LANGUAGE_CODE }}"/>
</form>
<ul role="menu" class="dropdown-menu"id="lang-dropdown">
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<li>
<a href="#" onClick='(function(){
document.getElementById("form_lang").value = "{{ language.code }}";
document.getElementById("form").submit(); return false;})();return false;'>
{{ language.name_local }} ({{ language.code }})</a>
</li>
{% endfor %}
</ul>
I created a special tag "strip_lang" to strip the language identifier from the current URL.
#register.filter
#stringfilter
def strip_lang(value):
"""Removes all values of arg from the given string"""
lang = getattr(settings, "LANGUAGES", None)
url = value.split('/')
if url[1] in [l[0] for l in lang]:
return urllib.parse.unquote('/' + '/'.join(value.split('/')[2:]))
else:
return urllib.parse.unquote(value)
This all works well. However, for one of my apps, I always get rerouted to a different app.
ie if I am at path
/en/app1/mypage1
and toggle the language i suddenly end up at
/fr/app2/mypage1
*****EDIT*****
Referals for app2 work correctly. When entering a URL manually for app1 the pages load correctly.
My url.py in the main project looks like this
urlpatterns = [
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += i18n_patterns(
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
path('app3/', include('app3.urls')),
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^login/$', auth_views.LoginView.as_view(), {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(), {'template_name': 'logged_out.html'}, name='logout'),
url(r'^oauth/', include('social_django.urls', namespace='social')),
prefix_default_language=False)
ans urls.py of my apps look like this
urlpatterns = [
path('', views.index, name='index'),
path('surveys/', views.nrgt_surveys, name='nrgt_surveys'),
path('surveys/<str:survey_name>/', views.survey, name='survey'),
path('surveys/<str:survey_name>/<query_name>', views.survey_query, name='survey_query'),
path('landscapes/', views.landscapes, name='landscapes'),
path('landscapes/<str:landscape_name>/', views.landscape, name='landscape'),
path('landscapes/<str:landscape_name>/<query_name>', views.landscape_query, name='landscape_query'),
]
I do not specify the app name anywhere in my code. When I track my network requests I can also see that the correct URL is submitted in the POST request. Why does my url get modified?

error while using "django.contrib.auth.views.login"

i am getting error "view must be a callable or a list/tuple in the case of include()." while trying to use django's built-in Login system (login,logout,logout_then_login). can anyone please sort this out.
bookmarks/accounts/urls.py-
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login',
name='logout_then_login'),
]
bookmarks/urls.py-
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/',include("account.urls"))
]
templates/registration/login.html-
<body>
<h1>Log-in</h1>
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% else %}
<p>Please, use the following form to log-in.
{% endif %}
<div class="login-form">
<form action="{% url "login" %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Log-in"></p>
</form>
</body>
templates/registration/logged_out.html-
<h1>Logged out</h1>
<p>You have been successfully logged out. You can <a href="{% url "login"
%}">log-in again</a>.</p>
</body>
I am assuming you are using django>1.9: So you cannot use strings as views anymore. So you need to do something like this with all views:
from django.contrib.auth.views import login
#.....
url(r'^login/$', login, name='login'),
...
And also with the include:
from django.contrib import admin
from accounts import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/',include(urls))
]

Django - How to get a form template to run (getting ' namespace not registered error ')

I am a newbie at Django.
Got Django 1.4.2 working with Python 2.7
on Windows 7.
I am following a tutorial and I need to get a form template appear
with radio buttons. Selecting a button sends data and the page should
ask you if you want to 'vote again?'. Also, if you do not select a button
and submit the form it should show a message asking you to make a choice.
So far,
here's the HTML form:
<h1>{{ poll.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' poll.id %}" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
Now when I type http://localhost:8000/polls/1/ I should get the form, but instead I get the following error message:
NoReverseMatch at /polls/1/
u"'polls" is not a registered namespace
I registered polls as a namespace, see below in the project urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
)
To answer cathy's kind request, here's the vote url:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
.. and below I include the project folder's urls if this could help:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
)
And I just noticed the following error message shown on the browser (because Debug = True):
Error during template rendering
In template C:\Python27\Scripts\mysite\mytemplates\polls\index.html, error at line 4
u"'polls" is not a registered namespace
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Any help to make that form run properly will be appreciated!
urlpatterns = patterns('polls.views',
url(r'^$', 'index', name='index'),
url(r'^(?P<poll_id>\d+)/$', 'detail', name='detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results', name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote', name='vote'),
)
{% url polls:detail poll.id %}
Go to your settings.py file and check out your template loaders. You may need to switch their order.
The correct order should be:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
Hope this helps.
Try to put {% load url from future %} on the top of your html file. It should fix your problem.