I'm using this library to handle two factor auth for a django project, but i'm having some troubles: in my site, i added a setup.html page, i set the url on my urls.py file but i keep getting this error:
In template C:\Users\Us\lib\site-packages\allauth\templates\base.html, error at line 26
Reverse for 'account_email' not found. 'account_email' is not a valid view function or pattern name.
<li>Change E-mail</li>
Which is completely weird because i'm not trying to load a file called base.html but my own setup.html file, which is located in my project's folder (the path is project-folder>templates>setup.html). This is the setup.html that i would like to load from my own templates:
{% extends 'main/header.html' %}
{% load i18n %}
{% block content %}
<h1>
{% trans "Setup Two-Factor Authentication" %}
</h1>
<h4>
{% trans 'Step 1' %}:
</h4>
<p>
{% trans 'Scan the QR code below with a token generator of your choice (for instance Google Authenticator).' %}
</p>
<img src="{{ qr_code_url }}" />
<h4>
{% trans 'Step 2' %}:
</h4>
<p>
{% trans 'Input a token generated by the app:' %}
</p>
<form method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.token.label }}: {{ form.token }}
<button type="submit">
{% trans 'Verify' %}
</button>
</form>
{% endblock %}
It looks like the module that i'm using, instead of loading MY setup.html will load something else, but i can't find a way to fix this.
Here is the view that i'm calling to handle the setup (it' the module's view):
https://github.com/percipient/django-allauth-2fa/blob/master/allauth_2fa/views.py
And here is my own urls.py, where the view that i mentioned is being called:
from django.urls import path
from . import views
from django.conf.urls import url, include
from django.conf.urls import url
from allauth_2fa import views as allauth_2fa_views
app_name = "main"
urlpatterns = [
path("setup/", allauth_2fa_views.TwoFactorSetup.as_view(), name="setup"),
path("", views.homepage, name="homepage"),
path("register/", views.register, name="register"),
path("logout/", views.logout_request, name="logout"),
path("login/", views.login_request, name="login"),
]
The TwoFactorSetup view is using a template setup.html in a folder allauth_2fa. So all you need to do is place your setup.html inside a folder with the same name: app_folder/templates/allauth_2fa/setup.html to override it.
Alternatively, subclass TwoFactorSetup and just change the template_name attribute to point to your template and use that view in your urls.py:
from allauth_2fa.views import TwoFactorSetup
class MySetup(TwoFactorSetup):
template_name = 'my_app/setup.html'
Related
I've been having this error for hours and I can't seem to fix it!
URL.PY:
from django.urls import path
from . import views
app_name = "learning_logg"
urlpatterns = [
#Home Page
path('', views.index, name="index"),
]
VIEWS.PY:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "index.html")
INDEX.HTML:
{% extends "learning_logs/base.html" %}
{% block content %}
<p> Learning Log </p>
<p> Learning Log helps you keep track of your learning, for any topic you're learning about gay </p>
{% endblock content %}
BASE.HTML:
<p>
<a> href = "{% url "index" %}"> Learning Log</a>
</p>
{% block content%} {% endblock content %}
I'm trying to make base.html the main framework and then putting index.html inside it, does anyone know how to fix the error?
It says the error is in line 2:
1
2 href = "{% url "index" %}"> Learning Log
3
4
5
6 {% block content%} {% endblock content %}
7
As you have specified app_name in your urls.py. You should do the same when referencing the url in the url template tag like {% url 'learning_logg:index' %}.
See the details in the documentation here.
I'm new to Django and I'm making wiki website based on markdown. I am having one problem. Django can't match the path in html to urls.py. It happens when I try to open wiki entries. It gives me the following error. I have already passed the parameter to 'page', I honestly have no idea what to do.
Using the URLconf defined in wiki.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
wiki/<str:page> [name='page']
search [name='search']
create [name='create']
addentry [name='add_entry']
nomatches [name='nomatches']
results [name='results']
edit/<str:page> [name='edit']
random [name='random']
The current path, { url 'page' entry }, didn't match any of these.
Please, tell me how can I fix this.
index. html:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<h1>All Pages</h1>
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
{% endblock %}
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:page>", views.viewpage, name="page"),
path("search", views.search, name="search"),
path("create", views.create, name="create"),
path("addentry", views.add_entry, name="add_entry"),
path("nomatches", views.search, name="nomatches"),
path("results", views.search, name="results"),
path("edit/<str:page>", views.edit, name="edit"),
path("random", views.random, name="random")
]
part of views.py:
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def viewpage(request, page):
content = util.get_entry(page)
return render(request, "encyclopedia/page.html", {
"content": markdown2.markdown(content),
"title":page
})
page.html
{% extends "encyclopedia/layout.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
edit
<h1>{{ title }}</h1>
{{ content|safe }}
{% endblock %}
Since the URL for 'page' is 'wiki/<str: page>' you need to define a get_absolute_url method on your class model that contains the object in question, so do add this method in the models.py class that contains page:
def get_absolute_url(self):
return reverse('page',kwargs={str:self.str})
Then, in the template, you can refer to it as
{% object.get_absolute_url%}.
I'm learning Django book named by (Django for beginners)
I have a problem with password _change
Below my code from urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('pages.urls')),
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/',include('django.contrib.auth.urls')),
]
Next is password_change_form.html
{% extends 'base.html' %}
{% block title %}
Password Change
{% endblock %}
{% block content %}
<h1>Password change</h1>
<p>Please enter your old password, for security's sake, and then enter your
new password twice so we can verify you typed it in correctly.</p>
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" class="btn btn-success" value="Change my password">
</form>
{% endblock %}
my content does'nt linking to password_change_form.html
Via versa linking to password_change from Django adminstration
Try this:
Crete an app named 'registration'
python manage.py startapp registration
Set it in the first position of your installed Apps:
INSTALLED_APPS = [
'registration',
....
]
Inside the app 'registration', create a the folder 'templates/registration'
Inside the folder created in the previous step, you can now set all your custom templates, 'password_change_form.html', 'password_change_done.html', etc....
import the urls inside the main urls, I would prefer using like this:
urlpatterns = [
... # your urls
path('accounts/',include('django.contrib.auth.urls')),
]
i'm using the django built in authentication system and in my login template i have this code :
login.html:
{% block title %}Login{% endblock %}
{% block content %}
<h2>Login</h2>
{% if user.is_authenticated%}
you are already logged in
{% else %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endif %}
{% endblock %}
but what i really want to do is to redirect the user to the home page if he tries to access login page while already logged in, but i am new to django so i don't know how to do that.
For django>=1.11 you can set redirect_authenticated_user parameter to True in its url in url_patterns to do the redirect, like this:
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
]
read the document for more information.
and also set LOGIN_REDIRECT_URL in your setting file to your index url or its name:
LOGIN_REDIRECT_URL = '/index/'
In your `settings.py' add this:
LOGIN_REDIRECT_URL = 'index'
if the url name of your index is 'index', else put the correct url name
You can do it in your views.py file.
def login(request):
if request.method =="get":
if request.user.is_authenticated:
return render(// youre code)
I have integrated djangocms blog in my project. In homepage sidebar i want to display latest blog posts. Problem is, my homepage is django-cms page and i can't get blog post objects. urls.py:
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
.....
url(r'djangocms_blog/', include('djangocms_blog.urls', namespace='djangocms_blog')),
url(r'^', include('cms.urls')),
)
Somehow here in main.html i should be able to get blog records:
{% extends "base.html" %}
<nav class="secondary-menu">
<ul class="nav">
<!-- i need display them here -->
</ul>
</nav>
Is there any elegant way to do this?
Use either an assignment_tag or an inclusion_tag:
Templatetag
# templatetags/blog_tags.py
from django import template
from djangocms_blog.models import Post
register = template.Library()
#register.assignment_tag()
def latest_posts_as(limit=5):
return Post.objects.order_by('-date_published')[0:limit]
#register.inclusion_tag('latest_posts.html', takes_context=True)
def latest_posts_inline(context, limit=5):
qs = Post.objects.order_by('-date_published')[0:limit]
context.update({'posts': qs})
return context
Snippet for inclusion tag
<!-- latest_posts.html -->
{% for post in posts %}
<p>{{ post }}</p>
{% endfor %}
Your home/whatever template
<!-- your_template.html -->
{% load blog_tags %}
<div>
<!-- using assignment_tag -->
{% latest_posts_as limit=20 as posts %}
{% for post in posts %}
<p>{{ post }}</p>
{% endfor %}
<!-- using inclusion_tag -->
{% latest_posts_inline limit=10 %}
</div>
The limit is optional - but could be handy :)
Came up with other solution. Created new django-cms plugin:
class BlogLatestEntriesPluginNav(BlogLatestEntriesPlugin):
name = _('Navigition latest blog entries')
render_template = 'latest_entries_nav.html'
plugin_pool.register_plugin(BlogLatestEntriesPluginNav)
where BlogLatestEntriesPlugin is plugin from djangocms_blog app. And created new template 'latest_entries_nav.html'. Then in main.html
<ul class="nav">
{% placeholder "left_sidebar" %}
</ul>
created placeholder and added that plugin.