Pycharm resolving references in Django templates - python

Is it possible for PyCharm to resolve references in Django templates? i.e., some code completion support, and when Ctrl + click on a template variable, we should go to the relevant python object.
The Jetbrains website boasts some impressive magic about Django templates:
Code completion works for Django tags, filters, template variables
and parameters passed from view methods.
The review on Dr Dobbs confirms this functionality. But I can't seem to get it to work:
I have setup the correct interpreter from my virtualenv, enabled Django Support in the settings and configured the templates directories, even invalidated and rebuilt the caches.
I'm on PyCharm v3.0.2.

In order to get this kind of functionality, you will need to see an icon on your views.py file, of the corresponding html file:
Using 3.0.2 here:
Either way, I will need to have a look at your directory and views.py file before I can tell you whats wrong with your source.

I had the same problem and I solved it by figuring out that my settings.py file was not configured in File > Settings > Languages & Frameworks > Django. Once I did that all intellisense clicked into place.
I've heard that this is a Pro-only feature but I cannot confirm that as I'm using a Pro license.

Related

Finding source of manage.py subcommand

I'm new to django and working on a modern django/Wagtail CMS app that is seeded using the command python manage.py loadfixtures. It seems like loaddata is the more common command, and I'm finding it incredibly difficult to find any documentation on loadfixtures at all. Could anyone point me towards the difference? It appears under the [Core] section of "available subcommands."
As an aside, I'm essentially trying to dump and seed some static page data for the site.
loadfixtures is not a command that exists as standard on either Django or Wagtail, so it's presumably a custom command that's been added by a developer on your project, or possibly a third-party package you have installed. See Writing custom django-admin commands for documentation on how these are created - if you have a 'core' app as part of your project, you may well find the code for the command in core/management/commands/loadfixtures.py.

Django : Why should I add package name inside INSTALLED_APPS?

In Djano, why should I add third-party package names inside the INSTALLED_APPS for some packages such as django-filter, DRF, debug-toolbar etc, while I don't want to add for some packages such as Celery, Requests etc ?
I couldn't figure out why should add them to a specific list, even though they all are similar pip packages and I installed them in the same way. Thanks in advance!
From docs :
Package? App?
A Python package provides a way of grouping related Python code for
easy reuse. A package contains one or more files of Python code (also
known as “modules”).
A package can be imported with import foo.bar or from foo import bar.
For a directory (like polls) to form a package, it must contain a
special file init.py, even if this file is empty.
A Django application is just a Python package that is specifically
intended for use in a Django project. An application may use common
Django conventions, such as having models, tests, urls, and views
submodules.
From above statements we understand that every well-written python code could be a package, now if this package is a bunch of python code in a directory, installing it would mean just copy the directory in your project and import them wherever needed in code.
But now there is this package which was already a django app and maybe used some special advantages of being one. Like exposing a restful API or delivering some sort of static resources or even working with special django-specific classes. these kind's of operation's need your direct permission in settings.py so that your project will know how to deal with this package. or even they may require to include their url path's to your project so that people visiting your site could access them.
I assume all this manual job is an act of security.
You need to add apps with models(non-abstract), templates, templatetags or management commands so that django-admin finds and loads them

Code coverage for jinja2 templates in Django

Following Ned Batchelder's Coverage.py for Django templates blog post and the django_coverage_plugin plugin for measuring code coverage of Django templates.
I would really like to see template coverage reports, but the problem is - we have replaced the Django's the template engine with jinja2 through the coffin adapter. I know it is not recommended, but we had reasons. Basically, instead of Django's render_to_response() we are using:
from coffin.shortcuts import render_to_response
Currently, we are using django-coverage's test_coverage command to run tests with coverage:
python manage.py test_coverage project_name
I understand that the setup is not common. But, is it possible to gather code coverage metrics for jinja2 templates with coverage.py? If not, I would appreciate any ideas on making a plugin similar to django_coverage_plugin, but for jinja2.
The plugin support in coverage.py is still in an alpha state. I've implemented the Django template support and half of the Mako support. A Jinja plugin would be appreciated. It might need changes in Jinja to make it feasible, in particular, to map the compiled Python code execution back to template line numbers.
If you want to start work on it, read the interfaces in plugin.py in the coverage.py source (I'll eventually write real docs, of course). The source for the Mako plugin might help also: https://bitbucket.org/ned/coverage-mako-plugin
This will likely involve us working together to get past the rough parts. Drop me an email! :)
Thanks for taking it on!
Updated: I've written some docs for the plugins, though more are needed: http://coverage.readthedocs.org/en/coverage-4.0a4/plugins.html

Does PyCharm support Jinja2?

A bottle project of mine uses Jinja2. PyCharm does not automatically recognize it and shows such lines as errors. Is there a way to make Jinja2 work?
In the pro edition, these template languages:
Jinja2
Django
Mako
are supported. You can configure the template language in the project's settings:
The community edition may lack certain template languages.
I think it's worth to mention that PyCharm Community edition does not support Jinja2, Mako and Django. It's available only in PyCharm Professional.
See comparison of the two.
Yes pro edition from pycharm does support Jinja2 to enable it go here
From File open Settings and search for python template under Languages & Frameworks Select Python Template Languages from there Click HTML And Select Jinja2 as Template Language.
please see the image for better understanding.
If you are using .jinja extension instead of .jinja2, it won't work, templates are not highlighted.
You have to add the file extension to the filetypes section.
Preferences > General > Filetypes
Scroll to Jinja 2 Template
Register new pattern by clicking +, add *.jinja
In community edition, the python template option is not available, so you can simply click on python packages next to the terminal present on the bottom. This will also add Jinja2

Any issues with using "<namespace>.django.<app-name>" for django applications?

I know Django applications are typically named django-appname, however, our non-django packages are released under a namespace for our company (eg. company.modulename). Our Django applications are currently structured in a "django" namespace within that namespace (eg. company.django.appname).
We are evaluating our structure before releasing Django apps. Regardless of the internal path structure, which do you think would be a better project name convention for Django apps and why?
"django-company-appname" <-- more like community naming
"company.django.appname" <-- matches the internal structure and non-django python packages we release.
Edit:
Let me make this a little more clear. We already are using namespaces--our django apps are imported using "company.django.appname". What we are trying to decide, is if the name we use to release that app, as defined in setup.py and listed in PyPI/Github will be "company.django.appname" like our other Python releases or if we should do "django-company-app" instead.
We use a company.modulename style namespace for a few django apps and have encountered no problems.
In the __init__.py files we declare the namespace.
import pkg_resources
pkg_resources.declare_namespace(__name__)
and it's worked fine so far.
Forcing absolute imports will remove any chance of issues due to reuse of package names.

Categories

Resources