Permanently redirect to another url in django - python

I have an application in django 1.5. I would like to do redirect from one address to another - permanently. That is, I want to get an effect in which I will redirect:
from:
domain.com/team/event-2019/01-03-2019/827
to:
domain.com/team/event/01-03-2019/827 - (without 2019)
traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/team/event/01-03-2019/827
Django Version: 1.5.4
Python Version: 2.7.12
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'haystack',
'users',
'adminextra',
'cms',
'pages',
'files',
'south',
'debug_toolbar',
'home',
'productions',
'contact',
'search')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'translation.middleware.LocaleMiddleware')
Traceback:
File "project/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "project/web/productions/views.py" in activity_details
284. return redirect(activity.get_absolute_url())
File "project/env/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in redirect
78. return redirect_class(resolve_url(to, *args, **kwargs))
File "project/env/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in resolve_url
151. return urlresolvers.reverse(to, args=args, kwargs=kwargs)
File "project/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
496. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "project/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
381. raise NoReverseMatch("Error importing '%s': %s." % (lookup_view, e))
Exception Type: NoReverseMatch at /team/event/01-03-2019/827
Exception Value: Error importing 'Content-Type: text/html; charset=utf-8
Location: /team/event/01-03-2019/827
': 'HttpResponseRedirect' object has no attribute 'rindex'.
I would like the event to be at the address: always under this url: domain.com/team/event/01-03-2019/827 and redirect on this url from: domain.com/team/event-2019/01-03-2019/827
Now I got an error:
'HttpResponseRedirect' object has no attribute 'find'
from this place:
if event_slug != activity.event.slug or str(date_url) != str(format_date(activity.date, "d-m-Y")):
return HttpResponsePermanentRedirect(activity.get_absolute_url())

Related

Exception Type: AttributeError at /notifier/not/ Exception Value: 'AsgiRequest' object has no attribute 'loop'

when i tried to run a piece if code in my django application i have been faced with some strange error
def main(request):
async def hello(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
msg="this is awesome"
ws.send_str(msg)
# return HttpResponse("ok")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop = asyncio.get_event_loop()
loop.run_until_complete(hello(request))
loop.close()
when i run this code
i just get
Exception Value: 'AsgiRequest' object has no attribute 'loop'
here is the complete traceback of the issue
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/notifier/not/
Django Version: 2.0.7
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest',
'accounts',
'connect',
'drones',
'notifier',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
'channels',
'oauth2_provider',
'social_django',
'rest_framework_social_oauth2',
'drfpasswordless',
'taggit',
'taggit_serializer',
'elasticsearch']
Installed Middleware:
['corsheaders.middleware.CorsPostCsrfMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.security.SecurityMiddleware',
'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']
Traceback:
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\madhumani\workspace\ven\drfc\notifier\views.py" in main
32. loop.run_until_complete(hello(request))
File "c:\users\madhumani\appdata\local\programs\python\python36-32\Lib\asyncio\base_events.py" in run_until_complete
467. return future.result()
File "C:\Users\madhumani\workspace\ven\drfc\notifier\views.py" in hello
23. await ws.prepare(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\aiohttp\web_ws.py" in prepare
106. protocol, writer = self._pre_start(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\aiohttp\web_ws.py" in _pre_start
183. self._loop = request.loop
Exception Type: AttributeError at /notifier/not/
Exception Value: 'AsgiRequest' object has no attribute 'loop'

Error in function "int() argument must be a string"

I have issues with the following code in my Django project.
Anyone one have a clue what's wrong?
Exception Type: TypeError at /tasks/2/3/ Exception Value: int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute'
(Django 2.1)
def step_detail(request, task_pk, step_pk):
user = request.user
steps = Step.objects.filter(task_id=task_pk)
step = get_object_or_404(Step, task_id=task_pk, pk=step_pk)
next = ""
if int(Step.step_number) < len(steps):
next = "/%s/%s/" % (task_pk, int(step_pk) + 1)
user.userprofile.current_step.update(step_pk)
else:
next = "/task_done/"
user.userprofile.current_step.update(step_pk)
user.userprofile.daily_task_done_time(datetime.datetime.now())
if request.POST:
if "nextstep" in request.POST:
return redirect(request.POST.get('next'))
return render(request, 'dailytask/step_detail.html', {'step': step})
Environment:
Request Method: GET Request URL: http://127.0.0.1:8000/tasks/2/3/
Django Version: 2.1.1 Python Version: 3.7.1 Installed Applications:
['django.contrib.admin', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.staticfiles',
'dailytask', 'account'] Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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']
Traceback:
File
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/core/handlers/exception.py"
in inner
34. response = get_response(request)
File
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/core/handlers/base.py"
in _get_response
126. response = self.process_exception_by_middleware(e, request)
File
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/core/handlers/base.py"
in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/contrib/auth/decorators.py"
in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/Users/iamsuccessful/ebdjango/dailytask/views.py" in step_detail
60. if int(Step.step_number) < int(len(steps)):
Exception Type: TypeError at /tasks/2/3/ Exception Value: int()
argument must be a string, a bytes-like object or a number, not
'DeferredAttribute'

Django TypeError on every page

I haven't touched the code on my app in weeks but i'm suddenly getting this exact same TypeError on every page, including Admin pages.
The last thing to happen was an update on a model to change a ForeignKey field, but I don't feel the two are related
Environment:
Request Method: GET
Request URL: http://10.0.0.161:8001/admin/
Django Version: 1.9.3
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'phones',
'jobs',
'profiles',
'freelancers',
'stock',
'finance']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'profiles.middleware.UpdateLastActivityMiddleware']
Traceback:
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
174. response = self.process_exception_by_middleware(e, request)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
172. response = response.render()
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/response.py" in render
160. self.content = self.rendered_content
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/response.py" in rendered_content
137. content = template.render(context, self._request)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/backends/django.py" in render
95. return self.template.render(context)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/base.py" in render
204. with context.bind_template(self):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" in __enter__
17. return self.gen.next()
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/context.py" in bind_template
260. updates.update(processor(self.request))
Exception Type: TypeError at /admin/
Exception Value: 'NoneType' object is not iterable
Solved it by looking through some of my own context processors. I had one function that was returning None on a request (after changing an entry and having an empty queryset as a result).

'WSGIRequest' object has no attribute 'mode' after adding easy_thumbnails context_porcessors

I'm trying to add to easy_thumbnails context processors in my settings.py and got the next error:
WSGIRequest' object has no attribute 'mode'
Context processors config:
TEMPLATES = [
{
....
'OPTIONS': {
'context_processors': [
....
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
'easy_thumbnails.processors.filters',
],
},
},
]
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:10801/uk/
Django Version: 1.8.3
Python Version: 3.4.0
Installed Applications:
('djangocms_admin_style',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'cms',
'adminsortable2',
'treebeard',
'menus',
'sekizai',
'bootstrap3',
'solo',
'mptt',
'modeltranslation',
'easy_thumbnails',
'filer',
'meta',
'meta_mixin',
'djangocms_page_meta',
'djangocms_inherit',
'djangocms_picture',
'djangocms_file',
'djangocms_teaser',
'djangocms_video',
'djangocms_link',
'djangocms_snippet',
'djangocms_text_ckeditor',
'catalog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware')
Traceback:
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
164. response = response.render()
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/response.py" in render
158. self.content = self.rendered_content
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/response.py" in rendered_content
135. content = template.render(context, self._request)
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/base.py" in render
208. with context.bind_template(self):
File "/usr/lib/python3.4/contextlib.py" in __enter__
59. return next(self.gen)
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/context.py" in bind_template
241. updates.update(processor(self.request))
File "/home/catalog_cms/.env/lib/python3.4/site-packages/easy_thumbnails/processors.py" in colorspace
64. if im.mode == 'I':
Exception Type: AttributeError at /uk/
Exception Value: 'WSGIRequest' object has no attribute 'mode'
Yes I'm using latest version of Pillow and easy_thumbnails.
I made a mistake, sorry. This is not context processors, but thumbnail processors. I just add:
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'easy_thumbnails.processors.filters',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
)
Now all working.

Filter iexact error Model matching query does not exist

I'm trying to verify if a value exists in my database with:
q = model.objects.filter(field__iexact='1')
But this makes an error in Django, which returns this message:
DoesNotExist at /answer_survey/
Model matching query does not exist
This is the entire traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/answer_survey/
Django Version: 1.6.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'survey',
'south',
'django_reset',
'chartkick',
'registration')
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')
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/filipeferminiano/Documents/django/panorama/survey/views.py" in answer_survey
265. s = Survey.objects.get(name=cod_survey)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py" in get
151. return self.get_queryset().get(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in get
307. self.model._meta.object_name)
Exception Type: DoesNotExist at /answer_survey/
Exception Value: Survey matching query does not exist.
Your error is here:
s = Survey.objects.get(name=cod_survey)
you have't records with name that contains in cod_survey variable...
Add try/except block to catch DoesNotExist exceptions:
try:
s = Survey.objects.get(name=cod_survey)
except Survey.DoesNotExist:
s = 'something else...'

Categories

Resources