Django context_processor no module named - python

I want to use a context_processors for my base.html template.
You can see my project structure in the following picture:
Project structure
from difoni_forms.DIFONIAPP.models import Admin
def check_active(request):
return {"active" : Admin.objects.get(id=1).active}
This is the code from my context_processors.py.And I added this line of code to my settings 'DIFONIAPP.context_processors.check_active'
When I run the program i get the following error:
No module named 'difoni_forms.DIFONIAPP'.
I don't understand what I am doing wrong any helpis appreciated, ty in advance.

Related

django-seo set up error - No module named seo

I am trying to add django-seo to a test project, but I have an error I don;t know how to solve.
I have followed the django-seo docs:
1. I ran pip install DjangoSEO and then confirmed the install with pip freeze;
2. I added 'rollyourown.seo' to my installed apps;
3. I created a file called seo.py and installed this file alongside my core files: models.py, views.py, forms.py & urls.py. This is the contents of my seo.py file:
from rollyourown import seo
class MyMetadata(seo.Metadata):
title = seo.Tag(head=True, max_length=68)
description = seo.MetaTag(max_length=155)
keywords = seo.KeywordTag()
heading = seo.Tag(name="h1")
4. I added the following code to my urls.py file to set up the admin:
from rollyourown.seo.admin import register_seo_admin
from django.contrib import admin
from test_project.seo import MyMetadata
register_seo_admin(admin.site, MyMetadata)
According to the docs, I should now be able to see the django-seo models in the admin, but do not see them.
Instead, I get the following error:
File "C:\Users\UserName\Desktop\test_project\test_project\core\urls.py", line 5, in <module>
from test_project.seo import MyMetadata
ImportError: No module named seo
Any suggestions would be great.

python / django's module / class theory: Template object under django.template.base.Template

I'm using Django 1.6.5, under the python manage.py shell, i create a Template object by:
from django.template import Template
t = Template('This is template {{ num }}.')
then i print t
the result is NOT <django.template.Template object at 0xb7d5f24c>, but
<django.template.base.Template object at 0xb7d5f24c>
Can someone explain why Template is just under django.template, but the created object is under django.template.base, and the module theory behind?
You can see this simply from the source code: the actual class is in django.template.base, but the __init__.py file in django.template imports the class so as to make it available via a more convenient name.

auth is not available in module

I have a web2py application where I have written various modules which hold business logic and database related stuff. In one of the files I am trying to access auth.settings.table_user_name but it doesn't work and throws and error as global name 'auth' is not defined. If I write the same line in controller, it works. But I want it to be accessed in module file. Please suggest how do I do that.
In your model file, where you define auth:
from gluon import current
auth = Auth(db)
current.auth = auth
Then in a module:
from gluon import current
def some_function():
auth = current.auth
....
For more details, see http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules.
I was getting a very similar error ("name 'auth' is not defined"). Had to add from django.contrib import auth at the top of views.py and it worked.

SiteProfileNotAvailable error

I know same question being asked many times. I have successfully setup all things. I didn't get this error when i run this on localhost. But when i run the site on deployment server i got this error not always but very often. When i refresh page 3 or 4 times the error disappears but keeps on coming again and again which is very annoying.
My directory structure related to problem is as:
project (folder)
profiles (folder)
models.py (file)
settings.py (file)
I have folder profilesin which there is models.py and i have class Profile there.
In settings.py i have included profiles folder to my apps also and i have set AUTH_PROFILE_MODULE = 'profiles.Profile'.
Also in Profile class i have added the path for application:
class Profile(UserenaLanguageBaseProfile):
class Meta:
app_label = 'profiles'
Can any one help me to figure out this issue??
AUTH_PROFILE_MODULE should be a string:
AUTH_PROFILE_MODULE = 'profiles.Profile'
I was using Django 1.4 trunck version, after upgrading the package again that problem is fixed.

Django: Getting list of all URL regexes [duplicate]

Is there a way to get the complete django url configuration?
For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.
Django extensions provides a utility to do this as a manage.py command.
pip install django-extensions
Then add django_extensions to your INSTALLED_APPS in settings.py. then from the console just type the following
python manage.py show_urls
Django is Python, so introspection is your friend.
In the shell, import urls. By looping through urls.urlpatterns, and drilling down through as many layers of included url configurations as possible, you can build the complete url configuration.
import urls
urls.urlpatterns
The list urls.urlpatterns contains RegexURLPattern and RegexURLResolver objects.
For a RegexURLPattern object p you can display the regular expression with
p.regex.pattern
For a RegexURLResolver object q, which represents an included url configuration, you can display the first part of the regular expression with
q.regex.pattern
Then use
q.url_patterns
which will return a further list of RegexURLResolver and RegexURLPattern objects.
At the risk of adding a "me too" answer, I am posting a modified version of the above submitted script that gives you a view listing all the URLs in the project, somewhat prettified and sorted alphabetically, and the views that they call. More of a developer tool than a production page.
def all_urls_view(request):
from your_site.urls import urlpatterns #this import should be inside the function to avoid an import loop
nice_urls = get_urls(urlpatterns) #build the list of urls recursively and then sort it alphabetically
return render(request, "yourapp/links.html", {"links":nice_urls})
def get_urls(raw_urls, nice_urls=[], urlbase=''):
'''Recursively builds a list of all the urls in the current project and the name of their associated view'''
from operator import itemgetter
for entry in raw_urls:
fullurl = (urlbase + entry.regex.pattern).replace('^','')
if entry.callback: #if it points to a view
viewname = entry.callback.func_name
nice_urls.append({"pattern": fullurl,
"location": viewname})
else: #if it points to another urlconf, recur!
get_urls(entry.url_patterns, nice_urls, fullurl)
nice_urls = sorted(nice_urls, key=itemgetter('pattern')) #sort alphabetically
return nice_urls
and the template:
<ul>
{% for link in links %}
<li>
{{link.pattern}} ----- {{link.location}}
</li>
{% endfor%}
</ul>
If you wanted to get real fancy you could render the list with input boxes for any of the regexes that take variables to pass to the view (again as a developer tool rather than production page).
This question is a bit old, but I ran into the same problem and I thought I would discuss my solution. A given Django project obviously needs a means of knowing about all its URLs and needs to be able to do a couple things:
map from a url -> view
map from a named url -> url (then 1 is used to get the view)
map from a view name -> url (then 1 is used to get the view)
Django accomplishes this mostly through an object called a RegexURLResolver.
RegexURLResolver.resolve (map from a url -> view)
RegexURLResolver.reverse
You can get your hands on one of these objects the following way:
from my_proj import urls
from django.core.urlresolvers import get_resolver
resolver = get_resolver(urls)
Then, you can simply print out your urls the following way:
for view, regexes in resolver.reverse_dict.iteritems():
print "%s: %s" % (view, regexes)
That said, Alasdair's solution is perfectly fine and has some advantages, as it prints out some what more nicely than this method. But knowing about and getting your hands on a RegexURLResolver object is something nice to know about, especially if you are interested in Django internals.
The easiest way to get a complete list of registered URLs is to install contrib.admindocs then check the "Views" section. Very easy to set up, and also gives you fully browsable docs on all of your template tags, models, etc.
I have submitted a package (django-showurls) that adds this functionality to any Django project, it's a simple new management command that integrates well with manage.py:
$ python manage.py showurls
^admin/
^$
^login/$
^logout/$
.. etc ..
You can install it through pip:
pip install django-showurls
And then add it to your installed apps in your Django project settings.py file:
INSTALLED_APPS = [
..
'django_showurls',
..
]
And you're ready to go.
More info here -
https://github.com/Niklas9/django-showurls
If you want a list of all the urls in your project, first you need to install django-extensions
You can simply install using command.
pip install django-extensions
For more information related to package goto django-extensions
After that, add django_extensions in INSTALLED_APPS in your settings.py file like this:
INSTALLED_APPS = (
...
'django_extensions',
...
)
urls.py example:
from django.urls import path, include
from . import views
from . import health_views
urlpatterns = [
path('get_url_info', views.get_url_func),
path('health', health_views.service_health_check),
path('service-session/status', views.service_session_status)
]
And then, run any of the command in your terminal
python manage.py show_urls
or
./manage.py show_urls
Sample output example based on config urls.py:
/get_url_info django_app.views.get_url_func
/health django_app.health_views.service_health_check
/service-session/status django_app.views.service_session_status
For more information you can check the documentation.
Are you looking for the urls evaluated or not evaluated as shown in the DEBUG mode? For evaluated, django.contrib.sitemaps can help you there, otherwise it might involve some reverse engineering with Django's code.
When I tried the other answers here, I got this error:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
It looks like the problem comes from using django.contrib.admin.autodiscover() in my urls.py, so I can either comment that out, or load Django properly before dumping the URL's. Of course if I want to see the admin URL's in the mapping, I can't comment them out.
The way I found was to create a custom management command that dumps the urls.
# install this file in mysite/myapp/management/commands/urldump.py
from django.core.management.base import BaseCommand
from kive import urls
class Command(BaseCommand):
help = "Dumps all URL's."
def handle(self, *args, **options):
self.show_urls(urls.urlpatterns)
def show_urls(self, urllist, depth=0):
for entry in urllist:
print ' '.join((" " * depth, entry.regex.pattern,
entry.callback and entry.callback.__module__ or '',
entry.callback and entry.callback.func_name or ''))
if hasattr(entry, 'url_patterns'):
self.show_urls(entry.url_patterns, depth + 1)
If you are running Django in debug mode (have DEBUG = True in your settings) and then type a non-existent URL you will get an error page listing the complete URL configuration.

Categories

Resources