How to use mezzanine together with allauth? - django

I commented mezzanine mezzanine.accounts from settings.INSTALLED_APPS and use allauth as AUTHENTICATION_BACKEND
settings.py
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend"
)
urls.py:
urlpatterns += [
url(r"^$", include("movies.urls")),
url("^blog2", blog_post_list, name="home"),
url(r"^admin/", include(admin.site.urls)),
url(r"^movies/", include("movies.urls", namespace="movies")),
url(r"^faq/", include("fack.urls")),
url(r"^accounts/", include("allauth.urls")),
url(r'^captcha/', include('captcha.urls')),
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
url(r'^tz_detect/', include('tz_detect.urls')),
url(r"^h/", include("home2.urls", namespace="home2")),
url(r"^profile/", include("profile.urls", namespace="profile")),
url("^", include("mezzanine.urls")),
]
The problem is that when I go to https://localhost/blog2, that I see the base.html of allauth.accounts.
Maybe mezzanine confuses allauth accounts/base.html with its own accounts/base.html? Or maybe I'm missing something else.

url("^blog2", blog_post_list, name="home"), uses blog/blog_post_list.html as template which extends base.html, but since there's no base.html inside blog/ app it used the first it could find which was in this case allauth, hence the confusion.
I.e. creating a base.html in <project_path>/templates/base.html, use e.g. mezzanine/core/templates/base.html as a template, solves it

Related

Django auth_views.PasswordResetView not sending custom email template

I'm trying to send a custom email template with the reset token. But it keeps sending raw HTML no matter what I do. I have previous experience of doing exactly the same thing without any problem with the Django and Django rest framework.
Here's how I did it,
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls')),
path('', include('django.contrib.auth.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
store/urls.py
...
path('password_reset',
auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name = 'registration/password_reset_email.html',
),
name='password_reset'
),
folder structure
...
templates
|-- registration
|-- html_password_reset_email.html
|-- password_reset_email.html
|-- ...
...
Note:
I tried deleting the password_reset_email.html template after that it sends the default Django email template.
Hoping for any guidance to solve the issue thanks in advance.
Finally days(3) of debugging I found my dumb mistake :(,
I was including all auth urls path('', include('django.contrib.auth.urls')) in my main urls.py file and re defining the auth.urls again in store/urls.py differently so the overriding doesn't happen correctly.
So, instead of,
path('password_reset', # <<< here
auth_views.PasswordResetView.as_view(
...
),
name='password_reset'
),
it should be,
path('password_reset/', # <<< here
auth_views.PasswordResetView.as_view(
),
name='password_reset'
),
Not gonna delete the question (^.^') #for_sake_of_dumb_fellows_like_me

Django, The current path, blog/, didn't match any of these

Please help with this
enter image description here
First, Welcome to Stack Overflow. You need to add codes to your question so that other users can have a background.
From what I see, there are two possibilities.
First, you have not added blog to the INSTALLED_APPS in the settings.py. To do that:
INSTALLED_APPS = [
...
'blog.apps.BlogConfig',
]
second possible problem is that you haven't added blog/ url to the list of urls. To do this, add the following to the urls.py file next to your settings.py
urlpatterns = [
url(r'^blog/', include('blog.urls')),
]
Then, in the blog app create a file named urls.py and then add the following:
from . import views
from django.conf.urls import url
urlpatterns = [
url(r'^$', views.blog_view, name='blog_view'),
]

Django URL namespaces - the template has to know its namespace?

I've been tinkering with (my first) Django project that started out with Django 1.6 and has just recently moved to Django 1.8. I've also been working through Django Patterns & Best Practices, learning how I should have structured it all along :-)
My project has several sub-apps and a typical main urls.py with lines like:
(r'', include('noc.apps.frontpage.urls')),
Within each app, I've prefixed all the URL names with the app name, e.g. frontpage_edit_page and used {% url %} throughout the templates to refer between views.
Then I read about URL namespaces, and thought I could de-uglify my URL names. As I first interpreted it, if I added a namespace to each include() in the main urls.py, everything would function as before, because the URL names referred to would all be resolved by the 'local' app. But it doesn't seem to work that way.
With this in the main urls.py:
(r'', include('noc.apps.frontpage.urls', namespace='frontpage', app_name='frontpage')),
This in the app urls.py:
url(r'^frontpage/edit/(?P<slug>[A-Za-z0-9]+)$', views.edit_page, name='front_page_edit_page'),
and {% url 'front_page_edit_page' slug=page.slug %} in a template inside that app, I get a NoReverseMatch exception with 0 URLs tried.
All the examples I can find are talking about prefixing URLs with the namespace - frontpage:front_page_edit_page but 1) how is this an improvement on the previous app prefix on the URL name? and 2) how could you ever have two instances of the same app, which is supposed to be a benefit... So I'm assuming that this is for linking between apps, not within apps.
So what is it that I am missing? Do I need to embed the app_name or namespace in my view function too? It's true that if I do prefix all my URL names with the namespace, even within the app, I get a rendered page, but it seems to defeat the point.
The way you're meant to do it is indeed to add the namespace to the URL tag;
{% url 'frontpage:edit_page' slug='SLUG' %}
But it would be better practice to structure your main project URls file like this;
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'frontpage', include('noc.apps.frontpage.urls', namespace='frontpage', app_name='frontpage')),
That way you can specify the path for each app in the main URLs file, and avoid repetition;
urlpatterns = patterns(
'noc.apps.frontpage.views',
url(r'^edit/(?P<slug>[A-Za-z0-9]+)$', 'edit_page', name='edit_page'),
With this you can introduce a RESTful URL structure to all your apps so you'll end up with things like;
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'frontpage/', include('noc.apps.frontpage.urls', namespace='frontpage', app_name='frontpage')),
url(r'contact/', include('noc.apps.contact.urls', namespace='contact', app_name='contact')),
url(r'myapp/', include('noc.apps.myapp.urls', namespace='myapp', app_name='myapp')),
All your apps can follow a similar structure then;
urlpatterns = patterns(
'noc.apps.contact.views',
url(r'^$', 'index', name='index'),
url(r'^add/$', 'add', name='add'),
url(r'^edit/(?P<slug>[A-Za-z0-9]+)$', 'edit', name='edit'),
urlpatterns = patterns(
'noc.apps.myapp.views',
url(r'^$', 'index', name='index'),
url(r'^add/$', 'add', name='add'),
url(r'^edit/(?P<slug>[A-Za-z0-9]+)$', 'edit', name='edit'),
Multiple instances of frontpage could be achieved using the top level namespace;
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'frontpage/', include('noc.apps.frontpage.urls', namespace='frontpage1', app_name='frontpage')),
url(r'frontpage/', include('noc.apps.frontpage.urls', namespace='frontpage2', app_name='frontpage')),
That way you should be able to target the top level instance namespace, followed by the app namespace like this;
{% url 'frontpage1:frontpage:edit_page' slug='SLUG' %}
{% url 'frontpage2:frontpage:edit_page' slug='SLUG' %}
But if you would like to keep your template links more generic I believe you can leave out the top level namespace and django will resolve to the current app which you have to add to the request object. This is detailed at the end of Reversing Namespaced URLs
For anyone to reference.
This worked for me, Django 2.2.2
# project urls.py
# DjpollsConfig is my app config
from djpolls.apps import DjpollsConfig
djpolls_app_name = DjpollsConfig.name
urlpatterns = [
path(djpolls_app_name, include('djpolls.urls', namespace='djpolls_app_name'))
]
# app urls.py
from django_proj.urls import djpolls_app_name
app_name = djpolls_app_name
# app template
{% url 'djpolls_app_name:detail' question.id %}
Hope it helps!
Try this
(r'', include('noc.apps.frontpage.urls', namespace = 'abc')),
and then in your templates:
{% url 'abc:frontpage:front_page_edit_page' slug=page.slug %}

Django language switch not working

I would like to translate URL prefix and also URL slug using django-modeltranslation where slug is saved inside database table. After switching the language i would like to stay on the same page and just change the language. I'm using form language switcher as described here:
http://docs.djangoproject.com/en/dev/topics/i18n/translation/#the-set-language-redirect-view
Problem is that the language is just switching on homepage. The other pages are just refreshed without language and URL change.
Is there any way how can i get current url in other language?
In root project urls.py i have following:
urlpatterns = patterns('',
# Examples:
(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^$', 'portfolio.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(_(r'^projects/'), include('projects.urls', namespace='projects')),
)
in app called projects i have urls :
urlpatterns = patterns('',
url(r'^$', all_projects, name='projects'),
url(r'^(?P<slug>[\w-]+)/$', project_detail, name='project_detail'),
)
If this is not a copy-paste-problem you're missing url function name in your main urls.py. Change line 3 of your provided code above to:
urlpatterns = patterns('',
...
# The following line need to be changed from
# (r'^i18n/', include('django.conf.urls.i18n')),
# to
url(r'^i18n/', include('django.conf.urls.i18n')),
...
)

django with dotcloud: part of URL skipped

This is a Django site, migrated to Dotcloud through this documentation. I have an issue with my URLs: I cannot access my admin part, and the root URL that is not supposed to be matched matches ! Let me explain in detail:
root/
|- settings.py
|- urls.py
|- champis/
|- urls.py
File root/urls.py:
urlpatterns = patterns('',
(r'^champis/', include('champis.urls')),
(r'^admin/$', include(admin.site.urls)),
)
File root/champis/urls.py:
urlpatterns = patterns('champis.views',
url(r'^$', 'index'),
url(r'^recherche/$', 'search'),
url(r'^glossaire/$', 'glossary'),
url(r'^glossaire/(?P<letter>\w)/$', 'glossary'),
url(r'^(?P<champi_name>\w+)/$', 'detail'),
url(r'^(?P<champi_name>\w+)/(?P<photo_nb>\d+)/$', 'detail'),
)
So I should find my admin site at http://server.com/admin, and my application at http://server.com/champis, but this is not the case:
http://server.com/admin and http://server.com/champis trigger a 404
but my applications is served at http://server.com !
It looks like as if the champis part of the URL was automatically and magically added to the root URL... Do you have an explanation ? Thanks !
EDIT: extract of my settings.py:
ROOT_URLCONF = 'urls'
Django version is 1.4, and actually DEBUG is set to True.
Remove the $ from '^admin/$':
urlpatterns = patterns('',
(r'^champis/', include('champis.urls')),
(r'^admin/', include(admin.site.urls)),
)