Error generation due to settings.py in django - python

-Created a project in django with 'CustomUser' model derived from 'AbstractUser' in an app 'users'
-Below is the settings file snippet.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'crispy_forms',
'users',
I get following error while running python manage.py makemigrations command with unsuccessful migration.
ValueError: The field authtoken.Token.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
After much trial and error, I have removed 'rest_framework.authtoken' from the Installed app and the migration error disappeared and my site become functional. Problem is I want to user rest_framework and token based authorisation but if I keep 'rest_framework.authtoken' in settings, migration is not taking place.
Please help

Related

Django don't makemigrations my apps after I install app in INSTALLED_APPS

Those are my installed applications:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'stu',
'stu2',
]
When using magane.py makemigrations I get those errors (I'm using Ubuntu):
ashish#ashish-MS-1050:~/Desktop/aculance/test1$ python3 manage.py makemigrations stu
No installed app with label 'stu'.
ashish#ashish-MS-1050:~/Desktop/aculance/test1$ python3 manage.py makemigrations stu2
No installed app with label 'stu2'.
I tried many times to apply migrations but it can't recognise my app
and gives me this error:
No installed app with label 'stu'.

Why swagger documentation doesn't work on PythonAnywhere domain?

I have used swagger to document my APIs on the Django rest framework, and deploy it on Pythonanywhere.
but the documentation URL doesn't have any response.
it is my urls.py code for swagger:
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="BoardGame API",
default_version='v1',
description="Test description",
terms_of_service="http://****.pythonanywhere.com/",
contact=openapi.Contact(email="contact#boardgame.local"),
license=openapi.License(name="TEST License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
...
path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
and my settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'drf_yasg',
'rest_framework_swagger',
'rest_framework_simplejwt',
'django_extensions',
...
]
Its because your static files has not been loaded on server try to collectstatic files and check in inspect of page whether css is loading or not
You have to learn about static in Django
When you apply it all UI related files will be created in a static folder
Then swagger UI will work on pythonanywhere
It's seems that static file is not loaded on live server:
step 1:
LOCAL= False
step 2:
python manage.py collectstatic
step 3:
Reset server
nginx: sudo systemctl restart nginx
gunicorn: sudo systemctl restart gunicorn

How to run Django 1.6 LiveServerTestCase without HTTPS?

I have a Django 1.6 project in which I'm trying to run LiveServerTestCase. However, I don't need HTTPS for my test case, so I'd like to disable it. Is there a way to do this?
I run:
$ website/manage.py test website/tests.py:MySeleniumTests
and Firefox opens pointed at:
https://localhost:8081/
I am able to run a secure server on port 8081 with:
$ website/manage.py runserver_plus --cert cert 0.0.0.0:8081
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'website.nucleus',
'website.blog',
'django_extensions',
'debug_toolbar',
'django_nose',
'django_coverage',
'tinymce',
'django_ztask',
'timezones',
'tracking',
'gunicorn',
'raven.contrib.django.raven_compat',
'social.apps.django_app.default',
'report_builder',
'bootstrapform',
'bootstrap3',
)
By default, Django uses http:// protocol for live server tests, quote from the source code:
class LiveServerTestCase(TransactionTestCase):
static_handler = _StaticFilesHandler
#property
def live_server_url(self):
return 'http://%s:%s' % (
self.server_thread.host, self.server_thread.port)
Since, you see https:// protocol used in tests, something is redirecting your requests to https. Check your MIDDLEWARE_CLASSES setting.
For example, django-ssl-redirect middleware could be the culprit.

Django: Error: Unknown command: 'makemigrations'

I am trying to follow the Django tutorial and I faced the following error when I enter python manage.py makemigrations polls
Unknown command: 'makemigrations'
Here's the link to the tutorial and I accomplished all the previous steps successfully and I am not sure what's going wrong now or how to fix it.
P.S.: I have already included "polls" in the INSTALLED_APPS!
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
'South',
)
Answer: I had to modify INSTALLED_APPS to :
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
and also used this command: python manage.py syncdb
Migrations were first added in version 1.7, officially released on September 2, 2014. You need to make sure your tutorial matches the version of Django you're working with. For instance, this version of the tutorial covers 1.9:
https://docs.djangoproject.com/en/1.9/intro/tutorial01/
Or, if you're using an older version of Django, you can change the "1.9" in that URL to whatever version you're on (back to 1.3). Or use the dropdown on the docs page to pick the version and search for "tutorial".
Find out what version of django you're running (thanks #BradyEmerson):
python -c "import django; print(django.get_version())"
If older than 1.8:
pip install --upgrade django
I was using version 1.9 and still getting this error. I had unapplied migrations and that was the root cause in my case. I ran 'python manage.py migrate' to apply them and it worked for me.
In django makemigration added after 1.7 so if you are using older version of Django then you have to change settings.py and add your application in installed app like
INSTALLED_APPS = (
'Demo',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
and then you can run command
python manage.py syncdb
You need to load the virtual environment before doing it.
Use below code for Linux/OSX:
source venv/bin/active
And the following code for Windows
source venv/Scripts/activate
I did following (for python version 3.6.4) to get this issue resolved:
install virtualenv
Activate virtualenv
Cheers
This worked for me: using ubuntu 20.04, Django 3.2
First after creating the model and addind to the setting, I did:
python manage.py makemigrations
I obtained the migration number here.
After that I did:
python manage.py sqlmigrate [NAME_OF_MODEL] [NUMBER_OF_THE_MIGRATION]
Finally:
python manage.py migrate
First time I add following piece of code into project_name\settings.py file.
`INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Django REST Framework
'rest_framework',
#Games application
'games.apps.GamesConfig',
]`
After save it, when run following code I got error.
`python manage.py makemigrations games`
Then I check the settings.py file I realize that there are two INSTALLED_APPS and second one has not followings. When I added these the code worked.
`#Django REST Framework
'rest_framework',
#Games application
'games.apps.GamesConfig',`

No module named pages.urls

I'm trying to install Django Gerbi-CMS and getting no luck. I've got through the installation a couple of times and not getting anywhere. When I run the server I get the error:
No module named pages.urls
When I install it tells me that it's installed correctly. However I look in the python2.7/site-packages and pages is not in there.
I installed on an existing site a while ago and I can't see what different other than pages and the other depencencies not showing in the above directory.
The last command I ran to install the package was:
$ sudo pip install django-page-cms
which like I said installed but nothing showing up in my site-packages.
Link to site - http://packages.python.org/django-page-cms/installation.html
SETTINGS:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
#'authority',
'pages',
'south',
'blog',
)
URLs:
if settings.DEBUG:
urlpatterns += patterns('',
# Trick for Django to support static files (security hole: only for Dev environement! remove this on Prod!!!)
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^admin_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.ADMIN_MEDIA_ROOT}),
)
urlpatterns += patterns('',
(r'^', include('pages.urls')),
)
If you need any other info please let me know.
Thanks!
After going through the installtion process several times I worked out that because I was using 'sudo' it was installing on the server and not inside the virtualenv!
So when you install the app you need to Install it without sudo, otherwise it will install outside of the virtualenv!
Did you included the application in your settings.py file in the section called
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'mptt',
'tagging',
'pages', # You have to include it here
)

Categories

Resources