I'm getting NoReverseMatch from the below view:
from django.shortcuts import render, redirect
def new_room(request):
label="test"
return redirect(chat_room, label=label)
# this didn't work either:
# return redirect('chat_room', label=label)
def chat_room(request, label):
...
My urls.py looks like this:
from django.conf.urls import url, include
from messaging import views
app_name="messaging"
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^new/$', views.new_room, name='new_room'),
url(r'^(?P<label>[\w-]{,50})/', views.chat_room, name='chat_room'),
]
Going straight to messaging/test/ will load the page correctly. It's only the redirect that is causing the issue.
Full stacktrace:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/messages/new/
Django Version: 1.10.5
Python Version: 3.5.2
Installed Applications:
['messaging',
'dal',
'dal_select2',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',,
'channels']
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 "g:\Python\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "g:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "g:\Python\lib\site-packages\channels\handler.py" in process_exception_by_middleware
240. return super(AsgiHandler, self).process_exception_by_middleware(exception, request)
File "g:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "g:\Git\Jam\messaging\views.py" in new_room
26. return redirect('chat_room', label=label)
File "g:\Python\lib\site-packages\django\shortcuts.py" in redirect
56. return redirect_class(resolve_url(to, *args, **kwargs))
File "g:\Python\lib\site-packages\django\shortcuts.py" in resolve_url
147. return reverse(to, args=args, kwargs=kwargs)
File "g:\Python\lib\site-packages\django\urls\base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "g:\Python\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
392. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /messages/new/
Exception Value: Reverse for 'chat_room' with arguments '()' and keyword arguments '{'label': 'test'}' not found. 0 pattern(s) tried: []
That's because you're using namespacing for your chat_room app.
Change to:
return redirect('messaging:chat_room', label=label)
Check here for namespacing and also here too.
Related
I'm developing a basic Django DRF API. I have been using class-based views with explicit URLs. Now I'm trying to use very vanilla DRF ViewSets + router (for auto URL generation).
Can anybody help point me in the right direction? I'm on DRF 3.8 and Django 2.0
Project urls.py:
from django.conf.urls import url, include
urlpatterns = [
url(r'^userprofile/', include('UserProfile.urls', namespace='UserProfile')),
]
App: UserProfile/urls.py:
from django.conf.urls import include
from django.urls import path
from rest_framework.routers import DefaultRouter
from UserProfile import views
app_name = 'UserProfile'
router = DefaultRouter()
router.register('humans', views.HumanViewSet,)
urlpatterns = [path('', include(router.urls))]
App: UserProfile/views.py
from rest_framework import viewsets
from UserProfile.models import Human
from UserProfile.serializers import HumanSerializer
class HumanViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = HumanSerializer
queryset = Human.objects.all()
I get an error:
__init__() missing 2 required positional arguments: 'app_name' and 'app_module'
Full traceback:
Including full traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/userprofile/humans/
Django Version: 2.0.4
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_framework',
'rest_framework.authtoken',
'rest_framework_simplejwt',
'django_extensions',
'django_pdb',
'django_use_email_as_username.apps.DjangoUseEmailAsUsernameConfig',
'custom_user.apps.CustomUserConfig',
'factory',
'UserProfile']
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 "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/rest_framework/viewsets.py" in view
103. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
466. request = self.initialize_request(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/rest_framework/viewsets.py" in initialize_request
125. request = super(ViewSetMixin, self).initialize_request(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in initialize_request
368. authenticators=self.get_authenticators(),
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in get_authenticators
262. return [auth() for auth in self.authentication_classes]
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in <listcomp>
262. return [auth() for auth in self.authentication_classes]
Exception Type: TypeError at /userprofile/humans/
Exception Value: __init__() missing 2 required positional arguments: 'app_name' and 'app_module'
I get this error in a project whith SuccessMessageMixin and and not know why. This is my code in view.py.
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic import CreateView
class CampanaNueva(SuccessMessageMixin, CreateView):
model = Campana
template_name = "licencias_campana_nueva.html"
fields = ['temporada', 'descripcion']
success_message = "a"
And Raise this error on save:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/licencias/editar/1
Django Version: 1.9.4
Python Version: 3.4.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'home',
'widget_tweaks',
'socios',
'equipaciones',
'licencias']
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',
'cc_corbelo.middleware.LoginRequiredMiddleware']
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\views\generic\edit.py" in post
279. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\views\generic\edit.py" in post
222. return self.form_valid(form)
File "C:\Python34\lib\site-packages\django\contrib\messages\views.py" in form_valid
14. messages.success(self.request, success_message)
Exception Type: TypeError at /licencias/editar/1
Exception Value: 'str' object is not callable
I have this function working in another project without problems...
I got exactly this same message when I accidentally used the messages framework incorrectly in the form_valid method of a completely different view in my project.
The correct code should have been:
messages.success(request, "Deactivated product")
But what I had written instead was
messages.success = "Deactivated product"
This code doesn't work, but it doesn't cause an error on the page, either! However, as soon as I submitted a different form that used the SuccessMessageMixin, I would see the 'str' object is not callable error.
(Having a problem on one page cause an error on a completely different page was fun to debug.)
I noticed this error while trying to access /admin/. Whenever /admin/model_name or /admin/ or /admin/* is accessed django says OverflowError at /admin/ and regular expression code size limit exceeded. I did a little digging and came across this SO post. I have checked urls.py of app since regex are mostly used in urls. But all I have is a typical admin url, nothing complex.
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
....
]
Stack trace
Environment:
Request Method: GET
Request URL: http://domain/admin/
Django Version: 1.9.8
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',
'rest_framework',
'haystack',
'myapp.apps.core',
'myapp.apps.home',
'myapp.apps.accounts',
'myapp.apps.dashboard',
'myapp.apps.app1',
'myapp.apps.app3',
'myapp.apps.app5',
'myapp.apps.app6',
'myapp.apps.app7',
'myapp.apps.app8',
'myapp.apps.app9']
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']
Traceback:
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/contrib/admin/sites.py" in wrapper
265. return self.admin_view(view, cacheable)(*args, **kwargs)
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
244. return view(request, *args, **kwargs)>
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)>
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/contrib/admin/sites.py" in index
505. app_list = self.get_app_list(request)>
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/contrib/admin/sites.py" in get_app_list
488. app_dict = self._build_app_dict(request)>
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/contrib/admin/sites.py" in _build_app_dict
473. current_app=self.name,
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
600. return force_text(iri_to_uri(resolver._revers>e_with_prefix(view, prefix, *args, **kwargs)))
File "/home/tuxbox/.virtualenvs/dekkho-qa/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix>
488. if re.search('^%s%s' % (re.escape(_prefix), pattern), candidate_pat % candidate_subs, re.UNICODE):
File "/usr/lib/python2.7/re.py" in search>
142. return _compile(pattern, flags).search(string)
File "/usr/lib/python2.7/re.py" in _compile>
242. p = sre_compile.compile(pattern, flags)
File "/usr/lib/python2.7/sre_compile.py" in compile
521. groupindex, indexgroup
Exception Type: OverflowError at /admin/
Exception Value: regular expression code size limit exceeded
We are using django+apache.
Any help is much appreciated.
TIA
I have managed to solve this by restarting apache2 with
sudo service apache2 restart.
Of course, I have inspected apache2 error log with
sudo tail-f /var/logs/apache2/error.log
But that gave me same output as stack trace. Aforementioned is a work around for this issue. I am not marking this question as solved since real issue have not been found yet.
Thanks.
I am trying to override the form for password_reset_form but it keep resulting in following error:
unhashable type: 'dict'
I interpret this error as password_reset_form does not exist for some reason?
My urls.py:
from rest_framework.routers import DefaultRouter
from django.conf.urls import patterns, url, include
from web import views
from web.forms import MyLoginForm, MyPasswordResetForm
urlpatterns = patterns('',
#url(r'^', views.IndexView.as_view(), name='index'),
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'authentication_form': MyLoginForm}),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^accounts/password/reset/$',
'django.contrib.auth.views.password_reset',
{'password_reset_form': MyPasswordResetForm},
{'post_reset_redirect' : '/user/password/reset/done/'}),
(r'^accounts/password/reset/done/$',
'django.contrib.auth.views.password_reset_done'),
(r'^accounts/password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm',
{'post_reset_redirect' : '/user/password/done/'}),
(r'^accounts/password/done/$',
'django.contrib.auth.views.password_reset_complete'),
)
My forms.py:
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm, PasswordResetForm
class MyLoginForm(AuthenticationForm):
username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Brugernavn'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Password'}))
class MyPasswordResetForm(PasswordResetForm):
username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Brugernavn'}))
Full stacktrace:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/password/reset/
Django Version: 1.6.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'web',
'api',
'django.contrib.admin',
'rest_framework.authtoken',
'corsheaders',
'django.contrib.auth')
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',
'corsheaders.middleware.CorsMiddleware')
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/views.py" in password_reset
142. post_reset_redirect = reverse('password_reset_done')
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
509. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
387. possibilities = self.reverse_dict.getlist(lookup_view)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse_dict
296. self._populate()
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _populate
273. for name in pattern.reverse_dict:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse_dict
296. self._populate()
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _populate
287. lookups.appendlist(pattern.name, (bits, p_pattern, pattern.default_args))
File "/Library/Python/2.7/site-packages/django/utils/datastructures.py" in appendlist
383. self.setlistdefault(key).append(value)
File "/Library/Python/2.7/site-packages/django/utils/datastructures.py" in setlistdefault
373. if key not in self:
Exception Type: TypeError at /accounts/password/reset/
Exception Value: unhashable type: 'dict'
Your arguments for url isn't right. Should be one dictionary
url(r'^accounts/password/reset/$',
'django.contrib.auth.views.password_reset',
{'password_reset_form': MyPasswordResetForm,
'post_reset_redirect': '/user/password/reset/done/'}),
Yesterday it worked fine when I ran the server but today I am getting this:
****Environment:
Request Method: GET
Request URL: http://192.168.2.206:8080/home/
Django Version: 1.1.1
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.markup',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.comments',
'mysite.registration',
'mysite.profiles',
'mysite.epw',
'mysite.remember_me',
'mysite.avatar',
'mysite.django_documents',
'mysite.inlines',
'mysite.blog',
'mysite.forum',
'tagging']
Installed Middleware:
('django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'mysite.remember_me.views.AutoLogout')
Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/pymodules/python2.6/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/utils/decorators.py" in _wrapped_view
48. response = view_func(request, *args, **kwargs)
File "/home/karthik/Desktop/EPW_LOCAL/mysite/../mysite/remember_me/views.py" in remember_me_login
81. result_of_update, result_of_category, result_of_cover, result_of_latest, result_of_block, result_of_footer, result_of_research = common_blocks()
File "/home/karthik/Desktop/EPW_LOCAL/mysite/../mysite/epw/views.py" in common_blocks
332. result_of_update = display_updated_date()
File "/home/karthik/Desktop/EPW_LOCAL/mysite/../mysite/epw/views.py" in display_updated_date
1357. updated_obj = pdf_database.objects.filter(updated_date__gte = previous_datetime, updated_date__lte = cur_datetime).order_by("-updated_date")[0]
File "/usr/lib/pymodules/python2.6/django/db/models/query.py" in __getitem__
159. return list(qs)[0]
**Exception Type: IndexError at /home/
Exception Value: list index out of range******
Your queryset has 0 results. specifically this one:
updated_obj = pdf_database.objects.filter(updated_date__gte = previous_datetime, updated_date__lte = cur_datetime).order_by("-updated_date")
yesterday its working fine when run server today i am getting like this
Add a new pdf_database object from the admin or console and this view should be back running fine like yesterday