Error after adding Django channels to installed_apps in settings file - django

I am just trying out django channels so I created a virtual environment and installed django, drf and channels. It threw error asking for visual c++ build tools which got resolved after installing it. Then I created a channels project and an app. Then just for testing I added a sample model as below and registered it with the admin. It compiled well and also I was able to see the model in the admin page.
My Model Calss
from django.db import models
# Create your models here.
class College(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=150)
objects = models.Manager()
def __str__(self):
return self.name
My admin.py
from django.contrib import admin
from .models import College
# Register your models here.
admin.site.register(College)
Now the Problem
I added channels to the INSTALLED_APPS list in the settings.py file like below,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channelApp',
'channels',
]
Now when I try to run the server using the runserver command I get the following error
ModuleNotFoundError: No module named 'win32api'
LookupError: No installed app with label 'admin'.
I have been searching but failed to find any suitable answer. Kindly help me out.
Thanks in advance.

Just after I posted this I stumbled onto a SO post
Issue after installing django channels
Just to reiterate in short, this is an open bug and the work around is to install the following package
pip install pypiwin32
After installing you may have to close and reopen the editor for the changes to reflect. And the error is resolved.

Related

Django app not showing registered models in Admin page

I have tried everything I can think of and find on here but nothing seems to be able to get my models to register and show on my Django Admin page. Weird thing is I have another model that is already appearing on the Admin page that I've matched the syntax for but that one works and this other one does not.
models.py
class SimpleModel(models.Model):
something = models.CharField('Simple Model', max_length=200, default='foobar')
def __str__(self):
return self.name
admin.py
from .models import SimpleModel
admin.site.register(SimpleModel)
settings.py
INSTALLED_APPS = [
'bootstrap4',
'app.apps.RequestsDashboardConfig',
'applications_dashboard',
'requests_dashboard',
#'applications_dashboard.apps.ApplicationsDashboardConfig',
'requests_dashboard.apps.RequestsDashboardConfig',
#"app.requests_dashboard",
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
You can see I've even tried multiple variations of the INSTALLED_APPS but even this still does not work. I don't even get any errors when I start the server with this series of commands after deleting the migrations.
python3 manage.py flush
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
python3 manage.py runserver 0.0.0.0:80
I even tried deleting the entire DB, recreating it, and then running this again and still no luck!
Not sure where I went wrong in the end but for anyone who encounters similar issues to mine, I was able to solve this by doing another complete DB rebuild.

error rebuilding the index using django-elasticsearch-dsl

I am trying to connect to a existing index in my local elastic search engine.
I am using django-elasticsearch-dsl package. I followed this tutorial to do that. https://github.com/sabricot/django-elasticsearch-dsl
please note that,I already developed my django app[website] with mysql database. I have some unstructured text data indexed in elastic-search. I want to develop this 'advanced search' HTML page for users querying data frpm elasticsearch.
I followed almost everything upto
python manage.py search_index --rebuild but once I excute it it asked Are you sure you want to delete the 'website_data_discovery' indexes? [n/Y]: when I say n it will be aborted.
when I say Y it gives a lengthy error saying django.db.utils.ProgrammingError: Table 'crdc.website_data_discovery' doesn't exist
This is my file structure.
crdc
website
->__pycache__
-> media
->migrations
->static
->templates
->__init__.py
->admin.py
->apps.py
->documnets.py
->forms.py
->models.py
->tests.py
->urls.py
->views.py
manage.py
This is my settings.py/crdc.
###more codes here
INSTALLED_APPS = [
'website.apps.WebsiteConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_elasticsearch_dsl',
]
ELASTICSEARCH_DSL={
'default': {
'hosts': 'localhost:9200'
},
}
###more codes here
This is documents.py/website
from django_elasticsearch_dsl import DocType, Index
from .models import Data_Discovery
data_discovery = Index('website_data_discovery')
data_discovery.settings( number_of_shards = 5, number_of_replicas = 1,)
#data_discovery.doc_type
class Data_DiscoveryDocument(DocType):
class Meta:
model = Data_Discovery
fields = ['extracted_text', 'source_type']
this is models.py/website
from django.db import models
class WebsiteRepository(models.Model):
##### mysql model
class Documents(models.Model):
#### mysql model
class Data_Deposite(models.Model):
#### mysql model
class Data_Discovery(models.Model):
## Elasticsearch model
Any help would be greatly appreciated. Thanks
I know this is late. But, I thought I should answer my own question, so that anyone face the same problem, can figure it out.
I created another App for Elastic search component.Yes, I created another App under the same project. And I followed this tutorial https://pypi.org/project/django-elasticsearch-dsl/
Then I called Elasticsearch view functions at views.py from the website App. It works perfectly now.
I am not a expert in Django or Elastic Search. So, I can not explain why it is not working as the previous attempt. I hope this helps to anyone who comes to this kind of situation.

django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: thumbnail

I am developing a django web project that uses the following packages/applications:
sorl-thumbnail
django-oscar
Here is a snippet of my settings.py file:
INSTALLED_APPS = [
'registration', #should be immediately above 'django.contrib.auth'
'django.contrib.auth',
# ...
'zinnia',
'zinnia_tinymce',
'sorl.thumbnail',
'embed_video',
# ...
'django.contrib.flatpages',
'compressor',
'widget_tweaks',
] + get_core_apps()
When I comment out sorl.thumbnail, I am able to run the development server using manage.py runserver. However, if I uncomment the sorl.thumbnail line and try to run the development server, it throws an exception:
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: thumbnail
Now, I am aware that a similar question exists on this site, however, following the instructions in the accepted solution, i.e.:
create a sol_thumbnail folder in same directory as the manage.py script
create sorl_thumbnail/apps.py (see below)
modify myproject/mysite/___init____.py (see below)
sorl-thumbnail/apps.py
from django.apps import AppConfig
class SorlthumbnailConfig(AppConfig):
name = 'sorl-thumbnail'
label = 'sorl.thumbnail'
myproject/mysite/_init _.py
default_app_config = 'sorl-thumbnail.apps.SorlthumbnailConfig'
Why is the fix above not working, and how do I resolve this issue?
BTW: I am using django-1.10
I went though the same problem of duplicated applications, and following exactly the similar question I solved my problem.
The problem with your solution is that you have added default_app_config = 'sorl-thumbnail.apps.SorlthumbnailConfig' to myproject/mysite/___init____.py, but you should have added to myproject/sorl-thumbnail/___init____.py.

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

Sorl thumbnail not showing thumb in AdminImageMixin

I'm having trouble with Django 1.3 using django-grappeli and sorl-thumbnail.
I have a project that uses this code from the official sorl-thumbnails docs:
# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel
from sorl.thumbnail.admin import AdminImageMixin
class MyModelAdmin(AdminImageMixin, admin.ModelAdmin):
pass
This project works well with the debug server and a nice little thumbnail appears in the change form of the admin.
However, in another project, i'm serving my project through WSGI and I have 3 separate domains:
www.example.com
media.example.com (that's serving user uploaded files)
static.example.com (that's serving static files)
However, in this project, the AdminImageMixin works fine except no thumbnail is available in the changeform for a model:
It uploads the picture in the correct place
It puts the correct text in the database field (uploads/ + picture_name.jpg) (i verified this with phpmyadmin)
It doesn't show any thumbnail in the form besides the browse button (like i'm used to)
Here is some sample code:
# models.py
class Category(models.Model):
name = models.CharField(max_length=200, verbose_name='name', help_text='Name of category')
description = models.TextField(verbose_name='Description', help_text='You can use Textile')
icon = ImageField(upload_to='uploads/', blank=True, null=True)
# admin.py
class CategoryAdmin(AdminImageMixin, admin.ModelAdmin):
pass
admin.site.register(Category, CategoryAdmin)
# settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'grappelli',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
'django_evolution',
'django_extensions',
'sorl.thumbnail',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Any ideea what I'm doing wrong?
Thank you in advance.
Did you remember to put the sorl.thumbnail in your INSTALLED_APPS and sync the database after it. In case you didn't there isn't a table for the key value pairs and it wont work. I suppose you are using the default database as your key value storage, not redis.
I run into the same problems, turns out the PIL I installed didn't have jpeg support to make the actual thumbnails although it never showed any error.
This is how I fixed it:
install jpeg support
sudo apt-get install libjpeg libjpeg-dev
On MAC:
brew install jpeg
reinstall PIL
pip install -I PIL
After recompiling it should show that the jpeg support is available, refresh your admin page and you should see the thumbnails.
Check with a debugger if the form field is using the correct widget.
I had the same problem when i was inheriting from 3 different admin classes:
django-mptt : MPTTModelAdmin
django-modeltranslation : TranslationAdmin
sorl-thumbnail : AdminImageMixin
I'm pretty sure that (in my case) django-modeltranslation is overriding the behaviour of sorl-thumbnail changing the "widget" attribute of the ImageField field from AdminImageWidget.
I forced the widget to AdminImageWidget on the get_form function like this:
def get_form(self, request, obj=None, **kwargs):
kwargs = self._do_get_form_or_formset(request, obj, **kwargs)
form = super(CategoryAdmin, self).get_form(request, obj, **kwargs)
form.base_fields['background'].widget = AdminImageWidget()
return form
At this point using the mixing AdminImageMixin is optional.
./manage.py makemigrations thumbnail
./manage.py migrate thumbnail
If you get permission issue look at
https://stackoverflow.com/a/41541666