Changing the CSS on the Django admin page - django

I am trying to change the admin HTML templates but I don't really now how to find the already created Django templates in the first place.
Is there an easy way to find all the templates?
Right now I am making new html templates and extending them to Djangos templates.

If you just look for an easy way to change the admin template you can use the django-admin-interface. You can change colors and it looks a bit more attractive. This works in my case pretty good.
pip install django-admin-interface

Is there an easy way to find all the templates?
Yes, they're all in the django/contrib/admin/templates/admin. You should be able to find the top-level django directory in the site-packages directory where you installed your Python packages. For example, if you created a .venv directory, it could be in .venv/lib/python3.8/site-packages/.
Otherwise, you could find the templates in the Github repository here.
The Django guide recommends copying those templates over to your own templates directory.

Related

Django INSTALLED_APPS 'polls' vs 'polls.apps.PollsConfig'

In every youtube tutorial I'v seen people simply add "app_name" to INSTALLED_APPS list.
Yesterday I started Official Django Tutorial and they suggest "app_name.apps.App_nameConf" notations.
Im guessing official way is a better way and it's not hard to memorize additional syntax, I just wanna be sure. Because stuff works ether way.
Please give me a simple, easy to understand answer so even I could get it. Thanks!
You are right, following official way is a proper way, since the created video tutorials or third party blogs may refer to the older version of the django and such way is deprecated now.
Good luck in your beginnings!
In INSTALLED_APPS one can specify all apps used in this django project app.
However, you can create multiple apps in one project. By default this would result in separate directories name in root project directory.
As all apps are in root project directory - this allows to specify only (their) directory name in INSTALLED_APPS.
On the other hand, we can specify full path to project sub-app AppConfig, not relying on auto-discovering / that sub-app is located in root project directory etc.
Multiple apps in one project?
Yes, multiple apps of one project are usually tightly coupled to this specific project and cannot be used outside of it.
The main idea is separation of concerns - to split the logic into separate sections, each responsible for some part of project app.
When your app starts growing, more different logic is being added to it, models.py / views.py / forms.py etc starts growing, making it difficult to read and understand project structure as a whole.
Even in small project - separating into sub-apps may be very helpful in terms of organizing things.
There are different styles of how to organize your django app, like where should sub-apps directories be located (i.e. in root directory, or in sub-directory with project name), and apps and settings directories can be located not in root project directory. In such cases being able to specify path directly to AppConfig class is nice and also more explicit, which is better than implicit.
default_app_config is mostly used in reusable apps - apps intended to be distributed and used by other people - published to pypi. This allows just add their name in INSTALLED_APPS, not full path to their AppConfig.
You should not use it in your project unless you know that you need it.
The polls.apps.PollsConfig is the newer, more explicit way, and it better allows multiple configurations of the same app.
The reason why polls works is that polls/__init__.py likely has a
default_app_config = 'polls.apps.PollsConfig'
line.
You have to activate your model, and Django needs to know that you have your app installed.
So in settings.py you need to add 'polls.apps.PollsConfig' in INSTALLED_APPS topic because in your app.py file you have likely defined a class PollsConfig:
In settings.py
INSTALLED_APPS = [
'polls.apps.PollsConfig,
'django.contrib.admin',
'<other app...>',
]
In apps.py
class PollsConfig(AppConfig)
name = 'polls'

django modifying code under dist-packages v/s copying source to project folder

I am trying to reuse an existing django app, django-reviews. I set it up based on the instructions and the code is now under dist-packages.
>>> import review
>>> review
<module 'review' from '/usr/local/lib/python2.7/dist-packages/review/__init__.pyc'>
The other apps that I am writing are under my project's root directory, which is under version control along with requirements.txt
Problem
Some of the conventions are different between the app's code and my code. For instance,
1) the templates in the app extend {% base.html %} and my base.html is actually named {% "__base.html" %"}.
2) The url structure for signing in for the app is accounts/sign_in, but my current sign_in url is at "/log_in"
Question
Changing the code under dist-packages doesn't seem like a good solution, as it will be outside of my version control and it isn't in my project's home directory either. Changing my code to match the app's logic will be a problem if there is any other conflicting app in the future.
Should I instead use the source code as a reference and create a new app called review in my project's home directory?
What is the recommended approach when dealing with such issues?
Modifying code in the dist-packages directory is never a good idea. I only do it to debug packages I use, but never for a permanent change.
If you do need to change the source code of a package, you best fork the repository and make your changes. You can add it to your project the way you want it (as Git submodules, or just import it in your existing repository).
However, the idea of a django reusable app is that it's reusable for many purposes and you don't have to fork it to make it usuable for you. Maybe this app isn't configurable enough for you. You can try to contribute to this project to make it more configurable, so it's more accessible to more people. That's of course a little bit more work, but can be fun!

how to locate the template files of django-apps installed?

I am using Django Rest Framework in my project. I did pip install djangorestFramework.
Currently the template that is rendered is default ones that is shipped with djangorestFramework. I want to look into the template code so that I can override and customize to my needs.
It is specified in the documentation on how to override certain features/themes. But I want to change almost everything. so it would be great if I could have access to those templates to help build mine.
On a related note, how to find the templates directories of django-apps installed.
Maybe you could use django-debug-toolbar, you will have a template tab for every page where you can find all the used templates to build the page and for every template the absolute path is given.

How can I use coffee script in a Django project?

I'm a new developer on Django and I want to use some CoffeeScript in my dummy project. However, documentation about this topic isn't good and I want to know from the experts which is the best option.
I was looking at possibilities like compressor but it is still confused. I'd really appreciate some pieces of advice.
Thanks in advance.
For automatic compilation, there's django-coffeescript. For a more complete toolchain (a bit like Rails Sprockets) there's Gears, a stand-alone project with plugins for Django, Flask or compilation on the command line.
When I've used CoffeeScript with Django however, I've mostly preferred to set up compilation as a part of a build process (great to have one in any case), there I mostly used custom Paver scripts together with directory watching tools (like watchdog/kicker) to compile .coffee files to Javascript and putting them in my static directory.
What exactly is your question? Django will let you use any JS libary you want. Just add it your HTML pages or templates as you would do without Django.
If it is about serving of staticfiles, check this: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/

The correct place to put a markdown extension file in a django project?

I've created a markdown extension file (called mdx_xxx.py) for a django project I'm working on but I can't really decide where to put it.
The documentation says that the file should reside on the PYTHONPATH and I've seen several blog posts inviting to just put the file in the root directory of the project.
However, that seems like an odd place to me as I would rather see it in the related application directory but then it's not on the PYTHONPATH anymore.
Could some experienced django programmer shed some light on this issue?
Thanks
Requiring extension files to live directly on the Python path, and not inside any package, is (IMO) an unfortunate limitation of the Python markdown implementation.
If your extension is very specific to your project, I think putting it in the project root is the best option available.
On the other hand, if your extension is reusable in other cases, I would package it up with a simple setup.py and install it into a virtualenv using pip, like I do with all my other dependencies.
Not everything should be in your project. This is a requirement, a dependency. You could still package them together, and you'll need to put this at the top level, I think. That basically means importable from the same location as the project itself. Personally, I push everything to a virtualenv, so its nice and clean. If you do the same, you're deployment process should include putting both your project and any dependencies safely into that virtualenv. Otherwise, to whatever location you have in path.
If you are using standard markdown library from pip (pip install markdown) now at version 2.3.1, the extension can be anywhere. You just have to provide dotted path to it. The old-style - having it directly on the PYTHONPATH in module prefixed mdx_ still works as well.
I have it in my app code:django_file_downloads.mdx_download.
To use it from django templates:
{% load markup %}
{{ variable|markdown:'django_file_downloads.mdx_download' }}