Reference 'django.contrib.auth.urls' to custom specific templates - django

I wrote specific templates to deal with password reset and change,
the file tree structures as:
In [3]: !tree /Users/me/desktop/Django/forum/user/templates
/Users/me/desktop/Django/forum/user/templates
├── registration
│   ├── logged_out.html
│   ├── login.html
│   ├── password_change_done.html
│   ├── password_reset_complete.html
│   ├── password_reset_confirm.html
│   ├── password_reset_done.html
│   └── password_reset_form.html
└── user
├── activate.html
├── failure.html
├── register.html
├── success.html
└── validate.html
The project urls is configured as:
# Project url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
url(r'^article/', include('article.urls',namespace='article')),
url(r'^user/', include('user.urls',namespace='user')),
url(r'^user/', include('django.contrib.auth.urls')),
]
Unfortunately, when I try to issue request as http://127.0.0.1:8001/user/password_reset/,
the browser redirect to its default admin site
I am working on Django 1.11
How reference 'django.contrib.auth.urls' to my own templates?

If you just want to override admin templates, you can just add admin directory and templates in your template dir.
It's well explained in django official docs (https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates)
If you want to override view either, you have to make your own app and match it to urls.

You can also pass template_name as variable...:
in urls.py:
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^user/login-custom-template/$', auth_views.login, {'template_name': 'path-to-custom-template.html'}, name='login-custom'),
]

To ref to your template, in case it doesn't work just use template name:
urlpatterns = [
path(
'change-password/',
auth_views.PasswordChangeView.as_view(template_name='change-password.html'),
),
]
OR:
Even skip django.contrib.auth.urls if you want to use another directory template not in registration, in this case I point to template /basic_app/
from django.contrib.auth import views as auth_views
AND
re_path(r'^login/$',auth_views.LoginView.as_view(template_name='basic_app/login.html'),name='login')

Analyzing the same problem while doing this tutorial:
I discovered that You can skip 'admin' route described earlier and just move Your users app above other apps in settings:
INSTALLED_APPS = [
'users',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# External apps
'taggit',
# My apps
'projekty',
'blog',
]

Related

Trouble with routing in django - page not found

I am not able to perform routing.
error at /home:
[enter image description here]
urls.py:
from django.contrib import admin
from django.urls import path
from app.views import views
urlpatterns = [
path('home/', views.home_view, name='home'),
path('admin/', admin.site.urls),
]
views.py:
from django.http import HttpResponse
from django.shortcuts import render
def home_view(*args, **kwargs):
return HttpResponse("<h1>Hello World<h1>")
From the limited information (at the time of writing this answer) provided by OP, it seems the django app app is not registered in INSTALLED_APPS
From the images, it seems the project is structured like this
project/
├── app
│   ├── __init__py
│   └── views.py
├── asgi.py
├── __init__.py
├── manage.py
├── settings.py
├── urls.py
└── wsgi.py
You need to add app in the INSTALLED_APPS list in your settings.py
INSTALLED_APPS = [
...
'app',
...
]
This activates your sub-app app to be used in your django project.
the import in urls.py should be
from app import views
but it is difficult to judge without the full error message

Django custom AppConfig breaks the project

I've got a bare-bones demonstration Django project created with Django 2.2. The structure looks like:
my_project/
├── db.sqlite3
├── my_app
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models.py
│ ├── urls.py
│ └── views.py
├── manage.py
├── my_project
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── requirements.txt
This started as a tiny issue: "my_app" was showing up in the Django admin as "MY_APP" (with an underscore). The Django docs say that you can make this prettier by defining a custom app config that subclasses "django.apps.AppConfig", and that typically this subclass would be defined in (using my example) "my_app/apps.py". In fact, using manage.py startapp my_app even generates this for you:
# my_app/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'my_app'
The docs also say to "put the dotted path to that subclass in INSTALLED_APPS". Another approach is to set the default_app_config variable in your app's "__init__.py", but that "New applications should avoid default_app_config. Instead they should require the dotted path to the appropriate AppConfig subclass to be configured explicitly in INSTALLED_APPS."
However, if I do add the path to the custom app config into INSTALLED_APPS like this:
# settings.py
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
'my_app.apps.MyAppConfig'
]
...
the project immediately crashes with "LookupError: No installed app with label 'admin'."
The error traces back to the reference to admin.site.urls in the project's root "urls.py" (line 6 below):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('my_app/', include('my_app.urls', namespace='my_app'))
]
In conclusion, adding the path to the "apps" module created by "startapp" -- even without
making any changes to that module -- crashes the project. OTOH if I go against the docs' advice and define default_app_config in "my_app/init.py", that does work.
This seems very basic, but Googling it is turning up nothing, which usually means I'm making some fundamental error that I don't realize. Any ideas, anyone?

django module import error - python 2.7 vs python 3.4

I have installed django1.9 with python 2.7. But now I want to use it with python3.4. Hence I have modified symbolic link of python to python 3.4 like below.
sudo ln -s /usr/bin/python3.4 /usr/bin/python
Because same django works with python 2.7 and 3.4 as well so it should work. But now if I run ./mange.py runserver I am getting below error. But with Python 2.7 the same code works properly.
from Helpers import views
ImportError: No module named 'Helpers'
Please let me know whats wrong there? Below are the project structure.
myproject
├── myproject
│ ├── settings.py
│ ├── __init__.py
│ ├── urls.py
│ ├── wsgi.py
│ └─── Helpers
│ ├── views.py
│ └── __init__.py
└── manage.py
Urls.py is like below.
from django.conf.urls import url
from Helpers import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
setting.py contains below relevant information.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myproject',
]
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Look for modules here as well.
sys.path.insert(0, os.path.join(BASE_DIR, "Helpers"))
Any idea?
Python 3 has changed the import policy. Take a look at this question.
Instead of adding Helpers directory to sys.path, add it's parent:
sys.path.insert(0, os.path.join(BASE_DIR, 'myproject'))
Or like #albar mentioned - use relative import:
from .Helpers import views

Django: Grappelli custom dashboard

I'm having problems with the Dashboard of Grappelli because I cannot see any change in my admin interface. What I'm trying to do is to show my models in different boxes. This is my configuration:
myproj
├── myproj
│ ├──url.py
│ ├──settings.py
├── manage.py
├── db_personal #myapp
│ ├── admin.py
│ ├── models.py
| ├── viewss.py
├── templates.py
├── dashboard.py
settings.py
INSTALLED_APPS = (
'grappelli',
'grappelli.dashboard',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"db_personal" #app
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request"
)
GRAPPELLI_INDEX_DASHBOARD = {'django.contrib.admin.site': 'db_personal.dashboard.CustomIndexDashboard'}
urls.py
urlpatterns = patterns('',
url(r'^grappelli/', include('grappelli.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Anny suggestion?
Did you see documentation ?
http://django-grappelli.readthedocs.org/en/latest/dashboard_setup.html
try define 'grappelli.dashboard' before 'grappelli' in your settings 'INSTALLED_APPS'
then, to cutomize dashboard you need to create the dashboard.py file (see doc)
I had a similar problem.
You just have to put 'grappelli.dashboard' before 'grappelli' in INSTALLED_APPS:
INSTALLED_APPS = (
....
'grappelli.dashboard',
'grappelli',
....
)
Finally all I had to do is sign out and log in again to see the changes.

My custom adapter isn't used by django-allauth

I'm using django 1.6.5 and django-allauth 0.18.0 and the social login works as expected once we create the social app in the django's admin panel.
So, my next step was trying to change the module's behavior by using adapters.
It looked simple in the docs but somehow, I can't seem to make django-allauth use my custom adapters.
So here's my attempt of trying to pdb into my adapter's methods.
here's my folders/files structure:
.
├── manage.py
├── requirements.freeze
├── foo
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── foo_app
├── adapters.py
├── views.py
├── etc...
here's my foo/settings.py file:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sites',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'foo_app'
)
ACCOUNT_ADAPTER="foo_app.adapters.FooAppAccountAdapter"
SOCIALACCOUNT_ADAPTER="foo_app.adapters.FooAppSocialAccountAdapter"
And here's my foo_app/adapters.py file:
# -*- coding: utf-8 -*-
import pdb
from allauth.account.adapter import DefaultAccountAdapter
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
class FooAppAccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=true):
print "FooAppAccountAdapter.save_user"
pdb.set_trace()
return super(FooAppAccountAdapter, self).save_user(
request, user, form, commit
)
class FooAppSocialAccountAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
print "FooAppSocialAccountAdapter.pre_social_login"
pdb.set_trace()
return super(FooAppSocialAccountAdapter, self).pre_social_login(
request, sociallogin
)
def save_user(self, request, sociallogin, form=None):
print "FooAppSocialAccountAdapter.save_user"
pdb.set_trace()
return super(FooAppSocialAccountAdapter, self).save_user(
request, sociallogin, form
)
None of my set_trace are working and I think I might just have forgot something in the settings but can't figure it out.
So what I am missing or doing wrong guys ?
You have to add
ACCOUNT_ADAPTER = 'project.users.adapter.MyAccountAdapter'
to the setting.py
for more details on customising the adapter class, check this link https://django-allauth.readthedocs.io/en/latest/advanced.html#custom-redirects