If I try to add blog.apps.BlogConfig string to INSTALLED_APPS (to define the blog app), local server won't start giving me an error. Photos attached below:
I am expecting for the separate section to appear on site.
Was doing it by tutorial of the Youtuber Corey Schafer:
https://www.youtube.com/watch?v=qDwdMDQ8oX4&list=PL-osiE80TeTtoQCKZ03T
It's so simple.
Just put your application name in INSTALLED_APPS list.
Like, if you have application name "blog", then just put ...
INSTALLED_APPS = [
'blog',
...
]
That's it !
Still, if you need to put name of your application in INSTALLED_APPS list as "blog.apps.BlogConfig", then just go to apps.py in your application, and put ...
from django.apps import AppConfig
class BlogConfig(AppConfig):
name = 'blog'
Thanks !
Related
I'm working with Django 3.2 and trying to configure correctly the AppsConfig subclass of apps.py in order to avoid duplicate apps names when initzialing the project.
The context is the following. I have in my INSTALLED_APPS two apps whose names are equal although their paths not:
INSTALLED_APPS = [
...
'first_app.myapp',
'second_app.myapp'
]
To avoid the error shown below (and according to the documentation), I need to create an apps.py file subclassing AppConfig in at least one of the apps called myapp. I've decided to create that file in the second one, second_app.myapp.
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: myapp
The app.py in second_app.myapp module looks like as follows:
from django.apps import AppConfig
class MySecondAppConfig(AppConfig):
name = "second_app.myapp"
label = "my_second_app"
And in the __init__.py I've added:
default_app_config = 'second_app.myapp.apps.MySecondAppConfig'
My admin.py looks like:
from django.contrib import admin
from .models import MyModel
class MySecondAppAdminModel(admin.ModelAdmin):
list_display = ('attr_1', 'attr_2')
admin.site.register(MyModel, MySecondAppAdminModel)
When I start the project all works OK and I can use that model information, views called from second_app.myapp also work OK.
The problem comes when I access to the admin site (http://localhost:8000/admin), where only appears the first_app.myapp admin form instead of both.
Can you help me? Thanks in advance.
instead of this:
INSTALLED_APPS = [
...
'first_app.myapp',
'second_app.myapp'
]
Just try this way only:
INSTALLED_APPS = [
...
'first_app',
'second_app'
]
You got that error because here first_app.myapp and second_app.myapp myapp is duplicate. you should not use like this in both the apps.
OR
INSTALLED_APPS = [
...
'first_app.apps.MyFirstAppConfig',
'second_app.apps.MySecondAppConfig'
]
When creating an app with python manage.py startapp myapp, it automatically creates an apps.py file.
from django.apps import AppConfig
class MyappConfig(AppConfig):
name = 'myapp'
When I removed it, everything seems to work as before (at least my tests all passed). Is it a bad practice to remove these kind of files from the apps? Should we keep them to avoid side effects?
The recommended approach in Django is to use the app config in your INSTALLED_APPS:
INSTALLED_APPS = [
'myapp.apps.MyappConfig',
...
]
If you do this, then the apps.py file is required.
However, if you do not customize the app config at all, then it is possible to remove the apps.py if you use the app name in INSTALLED_APPS instead:
INSTALLED_APPS = [
'myapp',
...
]
I'm working in e-shop project using django-oscar and i trying to add COD support. I'm using django-oscar-cash-on-delivery.
I did the steps, you can see my configuration:
THIRD_PARTY_APPS = [
'jet.dashboard',
'jet',
'axes',
'cashondelivery',
'django_extensions',
'oscarapi',
'paypal',
'payu',
'rest_framework',
'robots',
'widget_tweaks',
'webpack_loader',
]
And created an app called apps and loaded properly:
INSTALLED_APPS = THIRD_PARTY_APPS + PROJECT_APPS + [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',
] + get_core_apps(
[
'apps.shipping'
]
)
In appsfolder i created an apps.py file with this code inside:
from oscar.app import Shop
# from apps.checkout.app import application as checkout_app
from cashondelivery.app import application as checkout_app
class ApplicationShop(Shop):
checkout_app = checkout_app
application = ApplicationShop()
But i can't understand why Oscar doesn't show me the checkout template with cash on delivery method.
This is my structure folder:
Can anyone help me with this?
There are two ways for integrate cash on delivery in django-oscar project:
Override checkout application:
In this case is necessary override checkout templates because by default Oscar don't or can't get the cashondelivery checkout templates, at least payment_details. So, first you need override these template file and after create an app and inside it add a app.py file where you override the checkout application as cashondelivery documentation says.
Create a custom view and checkout app:
You can see in django-oscar-paypal integration package a sandbox example for integrate paypal with Oscar.
You can see in this package that they use a checkout app for replace the defaul checkout app; inside this app they override view.py file and app.view file; override templates and override app.py file.
So, we can do the same for django-oscar-cash-on-delivery, so:
a. Create a new application called checkout inside the apps application if you want, or some like you. This application only will contain the views and application file.
b. In views file we'll put the cash-on-delivery views file, now it is in sanbox folder.
c. In app file inside checkout we declarate the checkout application:
from oscar.apps.checkout import app
from .views import PaymentDetailsView
class CheckoutApplication(app.CheckoutApplication):
payment_details_view = PaymentDetailsView
application = CheckoutApplication()
d. Finally, we declarate our checkout app as default checkout app as documentation says.
e. We have ensure that the application override default check out application, in settings:
INSTALLED_APPS = + get_core_apps(
[
'apps.checkout',
'apps.shipping'
]
)
You can now pay with cash on delivery method in your Oscar project.
We recommend use the second way because enable you use more than one method for payment.
PD:
Wich the new master version we have moved the views.py file at sandbox folder, so, the first option won't work anymore. But, if you want it come back, say us please!
I have installed the django-contact-form module and modified my urls.py file at the root level of my django app to contain:
from django.conf.urls import include, url
urlpatterns = [
# ... other URL patterns for your site ...
url(r'^contact/', include('contact_form.urls')),
]
as per the instructions listed here:
https://django-contact-form.readthedocs.io/en/1.2/quickstart.html
But when I try and run my Django app, I get 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.
I haven't added 'contact_form' to the INSTALLED_APPS setting as the guide explains that is not necessary.
Does anyone know what might be happening? Without that line in my urls.py file, the app runs fine.
Have you tried adding django.contrib.sites to the INSTALLED_APPS list in your settings.py?
I'm building a Django app and now I'm in production. I have this problem: after performing manage.py syncdb (all it's ok) I go into admin and I can not find the models tables . My admin.py file is present and this is my file url.py:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'stambol.views.home', name='home'),
# url(r'^stambol/', include('stambol.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)),
)
Where is the problem?
Permissions
First off, make sure you have permission to edit the missing models. It's common to be developing with a superuser account, and then to test your production deployment with a different non-superuser account. If you don't have at least read permission, the class won't be listed at all in the admin.
I think this is the most likely cause, but I will leave the rest since I had already written it.
Discovering your admin registrations:
One notable difference between runserver and a production server is that when you run runserver, it imports all your models.py files and validates the models. This does not happen in production, so if you register your models with the admin inside models.py you need to be sure to import that file so that code runs. You could do so in your main url conf.
The preferable solution is to do your registration in per-app admin.py files so they are picked up by autodiscover.
Settings:
You do need admin listed in installed apps, as #Pratik says. It also has some dependencies, as mentioned here. Installed apps should contain at least this:
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions'
'django.contrib.messages',
'django.contrib.admin',
#...
'myapp',
#...
]
Make sure the dir containing your app myapp is in your python path, so that myapp is picked up by autodiscover. This is working correctly for you already, or else you would get something like ImportError: No module named myapp.
Restarting the server:
Finally, just to recap what is buried deep within comments, you can restart your production server after making any code changes by touching your wsgi file: touch wsgi.py. Use tab-completion when you do this to be sure you're touching the existing wsgi file and not creating a new one thanks to a typo or some such. The wsgi file you're touching should contain something like this:
...
# tell django to find settings at APPS_DIR/mainsite/settings.py'
os.environ['DJANGO_SETTINGS_MODULE'] = 'mainsite.settings'
# hand off to the wsgi application
application = WSGIHandler()
Still broken?
If things still aren't working as expected, think farther outside the box. Keeping in mind that you're new to your production environment, is it possible some other code besides your own is being served up? Make some obvious change to a front-end page, restart the server, and see if it works. This is just a shot in the dark, of course.
from django import admin
from example.models import YourModel
admin.site.register(YourModel)
# or
class ModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
This should do the trick
Make sure you have the below removed as comments inside INSTALLED_APPS in settings.py. Then run ./manage.py syncdb again. They should like as shown below without the # in front of them
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',