Just upgraded to Django 1.4 and getting error with messages - django

I just upgraded to Django 1.4 and it has broken a couple things including messaging.
Here's the error I get when trying to change a avatar:
'User' object has no attribute 'message_set'
Exception Location: /Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/utils/functional.py in inner, line 185
Traceback:
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "/Users/nb/Desktop/spicestore/apps/avatar/views.py" in change
76. request.user.message_set.create(
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/utils/functional.py" in inner
185. return func(self._wrapped, *args)
Exception Type: AttributeError at /avatar/change/
Exception Value: 'User' object has no attribute 'message_set'
Also, messaging no longer works on the site. What are the changes in Django 1.4 that could be causing this and has anyone overcome a similar issue?

Django introduced a messages app in 1.2 (release notes), and deprecated the old user messages API.
In Django 1.4, the old message_set API has been removed completely, so you'll have to update your code. If you follow the messages docs, you should find it pretty straight forward.

Add
from django.contrib import messages
And then
def foo(request):
messages.add_message(request, messages.INFO, "Your message.")

What is in your INSTALLED_APPS in your settings.py?
Do you have 'django.contrib.messages', included there?
Something like:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
...

From Django 1.4 docs
To enable message functionality, in settings.py do the following:
Edit the MIDDLEWARE_CLASSES setting and make sure it contains
'django.contrib.messages.middleware.MessageMiddleware'
If you are using a storage backend that relies on sessions (the default), django.contrib.sessions.middleware.SessionMiddleware must be enabled and appear before MessageMiddleware in your MIDDLEWARE_CLASSES.
Edit the TEMPLATE_CONTEXT_PROCESSORS setting and make sure it contains
'django.contrib.messages.context_processors.messages'
Add 'django.contrib.messages' to your INSTALLED_APPS setting
As far as django-avatar is concerned. Use the master files found here: https://github.com/chadpaulson/django-avatar/tree/master/avatar

Related

wagtail pathoverflow on adding new child page

I have developed a wagtail site and that works just fine. It's just a simple blog. I was able to add a blog index page and blog posts under them. Hover recently when try to add a new page it gives the error
PathOverflow at /admin/pages/add/blog/blogpage/8/
Bellow is the complete traceback.
Environment:
Request Method: POST
Request URL: https://vikatakavi.info/admin/pages/add/blog/blogpage/8/
Django Version: 2.1.5
Python Version: 3.5.2
Installed Applications:
['home',
'search',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'blog']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware']
Traceback:
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/wagtail/admin/urls/__init__.py" in wrapper
102. return view_func(request, *args, **kwargs)
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/wagtail/admin/decorators.py" in decorated_view
34. return view_func(request, *args, **kwargs)
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/wagtail/admin/views/pages.py" in create
224. parent_page.add_child(instance=page)
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/treebeard/mp_tree.py" in add_child
1013. return MP_AddChildHandler(self, **kwargs).process()
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/treebeard/mp_tree.py" in process
387. newobj.path = self.node.get_last_child()._inc_path()
File "/home/ubuntu/projects/blogger/env/lib/python3.5/site-packages/treebeard/mp_tree.py" in _inc_path
1114. raise PathOverflow(_("Path Overflow from: '%s'" % (self.path, )))
Exception Type: PathOverflow at /admin/pages/add/blog/blogpage/8/
Exception Value: Path Overflow from: '000100010005ZZZZ'
I'm absolutely clueless on this.
To store a page's position in the page tree, Wagtail uses a technique called Materialized Path, which (in its standard settings) allows a maximum of 1679615 insertions at a particular point in the tree. It seems that you've hit this limit somehow. (The position at each point in the tree is represented as a 4-character alphanumeric code; the 'ZZZZ' as seen in the error message is the highest available code.)
You would run into this limit if you had >1.6 million blog posts, but obviously that's quite unlikely. A more likely possibility is that you've written an import or maintenance script that's performing a very large number of insertions or page movements - for example, if you had a script to randomly reorder pages, and you were running it on a section with 10000 pages, you would run out of index numbers after running it ~160 times.
If you really can't avoid hitting this limit, then you can increase the limit to 60 million by adding the following code somewhere where it will run on startup:
from wagtail.core.models import Page
Page.steplen = 5
However, this won't work on an existing database - you'll need to delete and recreate your database after making this change - and it means the maximum possible depth of the tree is reduced from 63 levels to 50.

<Model> matching query does not exist

I've learned python django since three days ago .
I wanted to remove an field from model, so I put the '#' front of that field.
Then I started the server again, but it gave me such an error message
'Userprofile matching query does not exist' (Userprofile is the model which I made.)
How can I solve this problem. I'm just a totally new to django, so even it's not easy to question my problem. If the clue is not enough, please tell me what other information is need.
p.s here is the total error message.
Environment:
Request Method: GET
Request URL: h ttp://localhost:8000/
Django Version: 1.6.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'admin_tools',
'admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.admin',
'south',
'autocomplete_light',
'djcelery',
'asap_web',
'facebook',
'venue',
'notification',
'study',
'general',
'member',
'advertise')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jaehoon/PycharmProjects/studysearch/asap_web/__init__.py" in __call__
89. return main()
File "/home/jaehoon/PycharmProjects/studysearch/asap_web/__init__.py" in main
25. return process_request()
File "/home/jaehoon/PycharmProjects/studysearch/asap_web/__init__.py" in process_request
53. return _call_proper_request_processor()
File "/home/jaehoon/PycharmProjects/studysearch/asap_web/__init__.py" in _call_proper_request_processor
77. return getattr(self, method_name)(request, *self.args, **kwargs)
File "/home/jaehoon/PycharmProjects/studysearch/general/views.py" in process_get_request
19. if user.is_authenticated() and user.get_profile().is_required_more_info:
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py" in get_profile
441. self._state.db).get(user__id__exact=self.id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in get
307. self.model._meta.object_name)
Exception Type: DoesNotExist at /
Exception Value: UserProfile matching query does not exist.
The exception is DoesNotExist which is thrown when you use get to retrieve single instance but it does not exist. In your process_get_request view:
user.get_profile().is_required_more_info
is throwing this exception because there is no profile created for this user. Note that user.get_profile() is deprecated since django 1.5 and you should use OneToOne field or custom user model instead, from docs:
get_profile() Deprecated in Django 1.5: Deprecated since version 1.5:
With the introduction of custom User models, the use of
AUTH_PROFILE_MODULE to define a single profile model is no longer
supported. See the Django 1.5 release notes for more information.

Django App template Loader, can't find app templates

I have the following two apps located in my virtualenv: backend backoffice.
The backoffice app has templates under:
backoffice/templates
So when I login into the Django shell, I can to do the following:
>>>from django.template.loaders.app_directories import Loader
>>> list(l.get_template_sources('index.html'))
[u'/var/www/venv2.7/lib/python2.7/site-packages/django/contrib/auth/templates/index.html', u'/var/www/venv2.7/lib/python2.7/site-packages/backoffice/templates/index.html', u'/var/www/venv2.7/lib/python2.7/site-packages/django/contrib/admin/templates/index.html']
So it seems the template is correctly found (l.load_template_source(('index.html') is also working).
However, when I access my home page via the browser I get an error:
TemplateDoesNotExist at /
Can someone help me solve the puzzle? What am I missing?
update: full trackback
Request Method: GET
Request URL: http://192.168.211.140/
Django Version: 1.5.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'backoffice',
'backend',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/contrib/auth/templates/index.html (File does not exist)
/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/contrib/admin/templates/index.html (File does not exist)
Traceback:
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
25. return view_func(request, *args, **kwargs)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/backoffice/views.py" in start_page
177. return render(request, 'index.html', context)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
170. t = get_template(template_name)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/template/loader.py" in get_template
146. template, origin = find_template(template_name)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/template/loader.py" in find_template
139. raise TemplateDoesNotExist(name)
Exception Type: TemplateDoesNotExist at /
Exception Value: index.html
Ah, I want to cry, how can Django be so tricky ...
I just updated settings.INSTALLED_APPS:
# It was like this
INSTALLED_APPS = (
'django.contrib.auth',
...
'backoffice',
'backend',
'django.contrib.admin' #!!! NOT ALLOWED HERE
)
# This will workd
INSTALLED_APPS = (
'django.contrib.auth',
...
'django.contrib.admin' # ALLOWED HERE
'backoffice',
'backend',
)
The order of INSTALLED_APPS is important. However, a good standard practice to prevent collisions on template names when you use the app directory loader is to namespace your templates by the app name in its director structure, such that:
Your templates for app backoffice live in:
backoffice/templates/backoffice/
In implementation, you would be rendering the template "backoffice/index.html" which will never collide with a template in django.contrib.admin. This design pattern plays nice with pluggable apps. Should you ever release backoffice as a pluggable app, other users can override its templates in their main template directory by simply adding a backoffice directory.

Django Type Error - render to string got multiple values for keyword argument context_instance

I'm new in django and I collect all the static and templates from the admin site to customize. Now I'm trying to render a queryset to the change_list.html from the admin site.
view.py
def person_list(request):
if request.method == 'GET':
qs = Person.objects.all()
return render(request, 'admin/change_list.html', {'app_label': 'clients'}, {'results_list':qs})
else:
return render(request, 'admin/change_list.html')
And I'm getting this type error:
render_to_string() got multiple values for keyword argument 'context_instance'
Here is the full traceback:
Environment:
Request Method: GET
Request URL: http://localhost:8000/clients/persons/
Django Version: 1.5.1
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'equipment',
'workers',
'clients',
'rents',
'bills',
'pays')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/facundo/dev/BLPServicios/blpservicios/clients/views.py" in persona_list
9. return render(request, 'admin/change_list.html', {'app_label': 'clients'}, {'results_list':qs})
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
Exception Type: TypeError at /clients/persons/
Exception Value: render_to_string() got multiple values for keyword argument 'context_instance'
Can you help me solve this? Thanks!
Try send values in one dictionary
{'app_label': 'clients', 'results_list':qs}
i think you are using the admin module to do something it can't... what are you trying to do?
i guess you'd better try to do it without the admin. often customizing it is more painful than implementing the code yourself, because of its complexity!
in particular, the {% result_list cl %} tag you see in the admin tamplate change_list.html (line 91) is not a template varible (that you are trying to pass in the context) but a template custom tag! whatever you want to do, this is the wrong path ;)

Site matching query does not exist. Lookup parameters were {'pk': 3}

Environment:
Request Method: GET
Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.linkedin',
'django.contrib.admin',
'django.contrib.admindocs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/var/www/sampleapp/allauth/account/views.py" in dispatch
41. **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/edit.py" in get
155. return self.render_to_response(self.get_context_data(form=form))
File "/var/www/sampleapp/allauth/account/views.py" in get_context_data
67. "site": Site.objects.get_current(),
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py" in get_current
26. current_site = self.get(pk=sid)
File "/usr/local/lib/python2.7/dist-packages/django/d
b/models/manager.py" in get
143. return self.get_query_set().get(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in get
401. (self.model._meta.object_name, kwargs))
Exception Type: DoesNotExist at /accounts/login/
Exception Value: Site matching query does not exist. Lookup parameters were {'pk': 3}
I am new to django,
Help me to fix this issue?
I encountered this problem as well. I was going through the PyCharm Django guide. I fixed this by deleting my sqlite database file and running syncdb again. I don't know why it didn't work the first time, but it works now.
Also, it's probably worth downloading the SQLite Database Browser so that you can see the actual structure of your SQLite database. For this issue, you're interested in the django_site table. After I deleted my database file and reran syncdb, there was one row in this table.
This problem occurs because the first time that you executes python manage.py syncdb. isn't specified a main user. To solve it, delete all tables created by django in data base (with mysql, postgresql..) or delete the database.db (with sqlite3), Then run python manage.py syncdb one more time