Django admin template overrides not working in production environment - django

Like this question, my admin overrides aren't working in my production environment but they are in my development environment (same django version). I've tried reordering the INSTALLED_APPS tuple in settings.py with no change (was the answer to the question linked above). Here's how I have my project constructed:
/WebDJ/ # project dir
+devices # unrelated app, but it uses templates (see below)
+sales
__init__.py
admin.py
models.py # has Customer and Transaction model classes
+templates
+admin
+sales
+Customer
change_form.html
+Transaction
change_form.html
+devices # lots of templates under here that work fine
404.html
500.html
also:
TEMPLATE_DIRS = ('/WebDJ/templates',)
is set in settings.py. The templates in the devices app are fine. What's not loading are the overrides in the admin directory - so the change form for Customer and Transaction has some extra stuff added to them (overriding the "after_field_sets" block).
Again, it works in my development environment (using PyCharm) but not in my production environment. Any ideas? I'm really stumped on this one.

Answer: on my production machine, apparently it didn't like "Customer" and "Transaction" despite that being the exact name of the models - it needed "customer" and "transaction".

Related

Why is the urls.py file not created automatically?

Almost every video which I saw about Django (for beginners), people who create applications using the startapp command and add their urls.py file manually in their application. My question is, if urls.py is so important for views and for our app why it's not creating automatically when we run startapp command!
Not every app directly serves the end user
URLs.py is only useful for routing users to pages which primarily have to do with that app. However, many apps may only do internal things. I have an app in one of my projects that handles badges and rewards, but there is no page which corresponds to any of that because it all shows exclusively as part of the profile pages (and the routing is handled within the profile app).
It just isn't always needed and that is why it is not always included.
Simply you don't have to serve each of your app to the end-users. You may have apps responsible for only your inner interactions. So it is not logical to put urls.py in each and every app.
It vary on how you use your routing.
django give project wide urls.py by default when you create the project using django-admin startproject command. so you can create all your project's urls on this file.
And not all app intended to server user directly using urls.
Whether i also prefer to create separate urls.py and api-urls.py routers for every app and include in main urls.py

Can we custom whole Django Admin site is it possible?

Is it possible to do anything in the django-admin site? I mean can we custom it according to our needs??
django-admin is an application added to the default django projects. As you can see in settings.py file, there's 'django.contrib.admin' and 'django.contrib.auth' (which contains user management models and logics) in the INSTALLED_APPS array. You can get rid of them if you want and add your-own developed alternative apps instead. But django-admin is a powerful tool and there's many guides to custom it's functionalities. For example django admin cookbook is a famous one.
It's all dependent on what you want and need to do.
Yes of course, you can start copying whole /your_python_directory/site-packages/django/contrib/admin/templates/admin to your local template directory. (defined in your settings.py)

How to enable per-site templates within Mezzanine multi-tenancy

We're expanding our business into Europe and I'm using Mezzanine's multi-tenancy feature to host both the US and EU versions of the site on the same Django installation. We have a /locations page on each site that I would like to serve with different templates, based on the SITE_ID.
I've followed Mezzanine's sparse documentation here and added the following to settings.py
HOST_THEMES = [
('domain.com', 'domain_app'),
('domain.eu', 'domain_eu')
]
I've added domain_eu to INSTALLED_APPS after the base theme and used python manage.py startapp domain_eu to generate the directory and manually created the domain_eu/templates/pages/locations.html file.
I then copied the locations page and assigned it to the EU site.
The page still renders with the locations template located in the base theme domain_app/templates/pages/locations.html
I've confirmed that the correct SITE_ID is set in the request.
How do I get the page to render with the template in its corresponding theme/app directory based on the current SITE_ID?
After digging into Mezzanine's code I figured out why my eu theme template was not rendering. The crucial bit of code can be found in mezzanine/utils/sites.py
def host_theme_path(request):
"""
Returns the directory of the theme associated with the given host.
"""
for (host, theme) in settings.HOST_THEMES:
if host.lower() == request.get_host().split(":")[0].lower():
When I logged the result of request.get_host() I quickly realized the problem because it was localhost which obviously would not match any of the HOST_THEMES domains.
I had assumed that Mezzanine would use the session variable site_id as per their documentation but apparently not down this particular template rendering code path.
The solution therefore was simply to edit my /etc/hosts file with the following:
127.0.0.1 domain.eu
Now when I visit domain.eu/locations it renders the template from the correct theme directory (in a local dev environment)

Multiple websites using the same app - how to set this up?

I have an app that shows products available in the US. If i want to change the country, I simply modify the value of a variable in my settings.py file.
Now... each country I serve needs to have its own site, e.g. example.co.uk, example.ca, etc. They'll all be hosted on the same server and use the same database. The views, static files,etc. would be almost the same for each country.
What's the best way of setting this up? Should I have one main app and then have per-country apps that extend the app?
(Using Django 1.6.2/Python 2.7)
I recently had something similar to do.
I have for each domain a specific setting file with an unique SITE_ID and also a wsgi file per site. Then in my http.conf (I'm using apache on webfaction) i set up multiple VirtualHost instances, each pointing out to the specific wsgi file.
My configuration looks something like this:
random_django_app/
__init__.py
models.py
...
another_app/
...
settings_app/
settings/
__init__.py
base.py
example_co_uk.py
example_ca.py
...
wsgis/
__init__.py
example_co_uk.py
example_ca.py
__init__.py
urls.py
Maybe try django-dynamicsites, not sure how well it will work with 1.6.2, I've used it on a project a few versions back. It worked great, you could override settings, urls and templates for each site and share everything else.
There is another project called django-dynamicsites-lite that looks a bit more recent.

How to use admin interface if I have no application?

I am creating a Django based app and I'd like to put everything under the root in the following structure:
/path/to/my/app/
settings.py
models.py
urls.py
admin.py
...
One problem that I run into is the admin interface doesn't include whatever models I have that are registerd in admin.py usin
admin.site.register(models.MyModel)
Usually that's done by using auto discover in urls.py, but now I have no registered "app", the auto discover doesn't work anymore. Is there anyway I can still use the admin interface?
Thanks.
Django simply doesn't work without apps. They're the fundamental building block of a Django site. A whole range of things, not just the admin, will fail to work. Why do you want to do this?
Putting the app in the django-style directory structure will make your project easily extensible if you decide to add functionality later.