Setting up django-allauth 404 error? - django

I am trying to get django-allauth to work for a project. In my Django project, lets call it yali, I did a git clone.
I then moved the folder /django-allauth/allauth to root /yali. and then deleted django-allauth and all the licenses, READMEs ...etc
According to documentation, there are three things I should do:
I should add this to settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"allauth.context_processors.allauth",
"allauth.account.context_processors.account"
)
AUTHENTICATION_BACKENDS = (
...
"allauth.account.auth_backends.AuthenticationBackend",
)
INSTALLED_APPS = (
...
'emailconfirmation',
'uni_form',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.twitter',
'allauth.openid',
'allauth.facebook',
And then add this to urls.py
(r'^accounts/', include('allauth.urls')))
Doing so, gives me a 404 when navigating to http://localhost:8000/account
What am I missing here? The documentation is somewhat unclear here and even potentially wrong. It instructs to point urls.py to "accounts", while there is no "accounts" folder but "account"

The folder name has nothing to do with the url. The urls of your apps are defined in urls.py. You can put what you want, but you must use those same urls when navigating in the browser.
In your case, you must navigate to:
http://localhost:8000/accounts
If you have defined urls as:
(r'^anything/', include('allauth.urls')))
you should navigate to:
http://localhost:8000/anything

Related

Why is Django is not loading my template view but it once was

My view was running fine until Ill tried to override an admin view. Which i eventually got to work. However in the process I broke the path to my view that was working. The admin view had nothing to do with my originally working view.
Now I copied my view into every possible level of my project structure. Yet the django template loader fails to find my order_input.html
The error shows the correct path to my order_input.html. I made copies of order_input.html at every single possible level of my project... but django still cant find it.
APP - URLS.PY
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hw/$', views.helloworld, name='helloworld'),
url(r'^order_input/$', views.order_input, name='order_input'),
url(r'^time/$', views.today_is, name='time'),
url(r'^$', views.index, name='index'),
]
SETTINGS.PY
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
# 'django.contrib.staticfiles',
'import_export',
'hw_app',
]
PROJECT URLS.PY
urlpatterns = [
url('r', include('hw_app.urls')),
url('admin/', admin.site.urls),
path('clearfile', views.clearfile),
path('readfile', views.readfile),
path('writefile', views.writefile),
path('helloworld', views.helloworld),
path('order_input', views.order_input),
path('ajax_view', views.ajax_view),
]
You have the order_input url defined in both your project and your app urls.py files, only in the hw_app version it has a trailing slash. The slashless project URL may be looking in the wrong places for things as it will assume there is a related view at its level.
Try removing that path from the project urls.py file. Assuming it should be going to the same place, it's already included when you include(hw_apps.urls) (as url() is just a more up-to-date path() ). Then try calling the page with a trailing slash.
The only view file then relevant should be hw_apps/views.py. For consistency place the HTML template file is in hw_app/templates/hw_app/order_input.html so you know django should be able to find it.

RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

I am building an application with Django Rest Framework and AngularJs. I am using Django-rest-auth for my authentication purposes, although, I have not been able to set it up. Anyway, I am trying to set up this app with my project. I realized I need to install django-rest-auth-registration to get it running, so I followed this documentation to do the following things:
I ran the commands
pip install django-rest-auth
and
pip install django-allauth
Any my settings.py looks like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 3rd party apps
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
# My app
'myapp',
]
I have also added the authentication backends, context_processors, and the proper urls.
However, when I try to migrate, my terminal throws the following error:
RuntimeError: Model class django.contrib.sites.models.Site doesn't
declare an explicit app_label and isn't in an application in
INSTALLED_APPS.
Why do I get this error, and how do I solve it to migrate my project? Thanks!
The fix
Just add Django's Sites framework to your apps and set SITE_ID to 1 in your settings.
INSTALLED_APPS = [
...
'django.contrib.sites',
]
SITE_ID = 1
Why does this happen?
Django's Sites Framework is a contributed module bundled with the core library that allows for the use of a single Django application/codebase with different sites (that can use different databases, logic in views, etc). The SITE_ID setting, as stated in the docs, "is used so that application data can hook into specific sites and a single database can manage content for multiple sites."
In this particular case AllAuth requires the Sites Framework in order to function properly. Many other third-party libraries are built to safely handle cases where multiple sites may be present and as such may be best .
I landed on this post via Google search. My problem was running tests that blew up with the error:
RuntimeError: Model class app.taxonomy.models.Term doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
This was running on Python 2.7.x with absolute imports. As mentioned by Colton Hicks in the comments, below, this can also happen with Python 3 (pytest 3.2.3 with Django 1.11.4).
In my tests.py:
from __future__ import absolute_import
[...]
from .models import Demographics, Term
After changing the relative import to an absolute import the problem went away:
from taxonomy.models import Demographics, Term
HTH
Try adding the app_label = 'yourApp' in the models Meta class:
class Meta:
app_label = 'yourApp'
I got the error above. However my problem was the in the urls.py. I was following PyDanny cookiecutter django recipe. My error was to put in the urls.py this line:
url(r'^demo/', include('project.demoapp.urls', namespace='demoapp')),
when I corrected to this:
url(r'^demo/', include('demoapp.urls', namespace='demoapp')),
all was well. I also changed my local apps (I did this first and so the critical error was the url misconfiguration):
LOCAL_APPS = [
# Your stuff: custom apps go here
'demoapp.apps.DemoAppConfig',
]
I have django debug toolbar installed and this was actually causing the/my problem.
INSTALLED_APPS (in settings.py) needs the entry 'django.contrib.sessions'. Make sure to run migrate after adding.
Just add 'django.contrib.sites', to INSTALLED_APPS and set SITE_ID = 1 in your settings.py file.
Upgraded Answer for Django>=4.0 // 2022
Add Django's Sites framework and FlatPages Framework to your INSTALLED_APPS and set SITE_ID in your settings.
INSTALLED_APPS = [
...
'django.contrib.sites',
'django.contrib.flatpages',
]
SITE_ID = 1
Your tests should work like a charm
This error occurred because I had created a new app folder for a subset of sites related to another feature. This needed to be added to my INSTALLED_APPS in settings.py
Django 4.1+ (2023)
After almost an hour digging, what solved for me was this:
INSTALLED_APPS = [
...
'django.contrib.sessions',
]
No need for SITE_ID or additional INSTALLED_APPS entries.
Everything worked as expected after I made a migration
python manage.py migrate
Good luck

Why is the Django-Simple-Captcha image not showing up?

I'm trying to add a Django-Simple-Captcha image to my application's login screen.
This is what I have added at the top of my forms.py file:
from captcha.fields import CaptchaField
This is what I have added to the registration form:
captcha = CaptchaField(
label="What does this say?",
required=True,
)
This is what I added to my site's url.py file:
urlpatterns += patterns(
'',
url(r'^captcha/', include('captcha.urls')),
)
I have also added "captcha" to my INSTALLED_APPS in settings.py
However, when I load the page, I see that the Captcha image is a broken link: http://predictstat.com/accounts/register/. The server shows this on the console:
[23/Dec/2013 16:30:47] "GET /captcha/image/56edd656ba57a2a3e71571373e1a59c564e3d592/ HTTP/1.1" 500 72336
However, there is no such directory "captcha" under the directory for my application. So where is it trying to look for this image? And why doesn't it exist?
On closer inspection, I found the issue was this:
Exception Value: The _imagingft C module is not installed.
Details how to solve this issue can be found here: Python: The _imagingft C module is not installed
There is no need to create any captcha directories.
The problem is that you did not update your urls.py as mensioned in the documentation:
urlpatterns += patterns('',
url(r'^captcha/', include('captcha.urls')),
)
Another problem could be that you did not run syncdb:
./manage.py syncdb
./manage.py migrate # If you use migrations
The same was happening in my application and I manage to make a very strange workaround (XGH alert) usising two slashes in django-simple-captcha 0.4.4
urlpatterns += patterns('',
url(r'^captcha//', include('captcha.urls')),
)
I think it may has something with the conflict of globally and virtureenv environment,may be there is another app which contains the name captcha.

Django admin DoesNotExist at /admin/

I have some problems with Django admin.
after syncdb, the result is:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
What does this mean?
Anyway, when I visit the website admin panel http://www.example.com/admin/, I receive this message:
DoesNotExist at /admin/
Site matching query does not exist.
setting.py contains:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
)
ur.py contains:
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'rshd.views.home', name='home'),
# url(r'^rshd/', include('rshd.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
You don't really need the sites framework if you only run one site from the project, so the easiest fix would be to remove the following item from your INSTALLED_APPS and the error should go away:
'django.contrib.sites'
You can also re-create the missing Site object from shell. Run python manage.py shell and then:
from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='www.example.com', name='example.com')
You can fix this error if you are using django.contrib.sites without removing it by changing SITE_ID = 1 value in settings.py file.
It is happen to me when I changed domain name on my server, I deleted old domain name from http://mynewdomainname.com/admin/sites/site/ manually, and the old domain record in database has id = 1, also I added new domain name mynewdomainname.com and it is id was id = 2,
I just changed to SITE_ID = 2 and the error gone SITE_ID refers to current "pk" of active domain name you use as default.
In code:
>>> from django.contrib.sites.models import Site
>>> # get_current() came from SiteManager class manager,
>>> # see site-packages/django/contrib/sites/models.py
>>> current_site = Site.objects.get_current()
>>> print(current_site.pk)
2
>>> print(current_site.domain)
'http://mynewdomainname.com'
this happen after settings.py changed
SITE_ID = 2
Be careful about current domain id in django sites app.
The same problem also suddenly came to me, and you know there're many solutions. However, what's the reason of this sudden problem?
After dig in deeply, I found the reason is that, we ignored a step in the first syncdb action.
When you have your first syncdb, django will ask you to create a default account, if you don't input in this interactive show, that site object would not be created automatically.
So be careful of this. I'm using django 1.3.1, don't know whether the latest version has resolved this issue.
Since the above comments are pretty old and the syncdb command doesn't exist, and I don't want to remove django.contrib.sites, here's what worked for me:
Increment SITE_ID. I had SITE_ID = 1, changed it to SITE_ID = 2 and everything worked again.
Add SITE_ID = 1 in your settings.py
just fixed the issue by an another way:
I use PostgreSQL and Django 1.45...
as i removed the www.example.com Site and added a new www.xxx.com Site it was added as ID=2
'cause PostgreSQL doesn't go back in the ID numbers and the login and logout Django-Sites are somehow bound only to the ID=1 Site in your DB...
I went to my PostgreSQL DB and changed the ID of www.xxx.com to 1 and then I was able to see the login and logout Site again :)
btw. [yes, you just can remove the django.contrib.sites from your settings.py if you don't need it ^^ (but I haven't tried this one out in my case, with the ID number problem)]
hope it will work for further users! ;)
Yes change the SITE_IT=1 to SITE_ID=2 and everything is ok
Yes, the solution mentioned at the top can be (part of) the solution.
I've noticed however, that not only including django.contrib.sites without configuration can cause that problem, also including any site of
allauth (pip install django-allauth) might cause this problem if said package is not configured correctly.
(And allauth is as far as I've seen not configured correctly by default...)
Another thing you can do is simple if you get some error or just want to change site.
Download sqlitebrowser
open your sqlite.db
search for django_sites
change domain name and display name to whatever you want
Check your Window task manager and make sure that there is 1 process name 'python.exe' is running. If there are many, delete all of them then restart the server. This solution works for me.

Should I remove 'django.contrib.comments' from my installed apps when I modify it by subclassing?

I'm customizing django comments.
According to the docs you'll need to add your customized app to INSTALLED_APPS in settings.py, and set COMMENTS_APP to your app name.
INSTALLED_APPS = [
...
'my_comment_app',
...
]
COMMENTS_APP = 'my_comment_app'
Should I also remove 'django.contrib.comments' from INSTALLED_APPS?
If you are only extending contrib.comments not replacing it, you shouldn't remove it from installed apps since, for example, most of the templatetags you need are in that application.
In order for Django to find the templates, templatetags and so on app must be in the installed apps.