Django HttpResponseRedirect and NoReverseMatch - django

I am trying to redirect to a new form page whose fields get automatically filled with the data sent from the view function.
My urls.py is:
url(r"user_dashboard/NewRequest/([\{.*\}])", views.request_form, name = "NewRequest")
The view function which is sending the data is present at:
url(r"user_dashboard/NReq", views.request_request, name="NReq"),
which calls the above url.
The return statement from my view function is:
return HttpResponseRedirect(reverse(request_form, kwargs={"device_type":devicetype}))
But, I get the following error:
NoReverseMatch at /user_dashboard/NReq
Reverse for 'inventory_management_app.views.request_form' with keyword arguments '{u'device_type': u'Laptop'}' not found. 1 pattern(s) tried: ['user_dashboard/NewRequest/([\\{.*\\}])']
Request Method: POST
Request URL: http://127.0.0.1:8000/user_dashboard/NReq
Django Version: 1.11.13
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'inventory_management_app.views.request_form' with keyword arguments '{u'device_type': u'Laptop'}' not found. 1 pattern(s) tried: ['user_dashboard/NewRequest/([\\{.*\\}])']
Exception Location: C:\Anaconda2\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable: C:\Anaconda2\python.exe
Python Version: 2.7.14
Python Path:
['E:\\inventory management\\inventory_management_service',
'C:\\Anaconda2\\python27.zip',
'C:\\Anaconda2\\DLLs',
'C:\\Anaconda2\\lib',
'C:\\Anaconda2\\lib\\plat-win',
'C:\\Anaconda2\\lib\\lib-tk',
'C:\\Anaconda2',
'C:\\Users\\Jaimik Jain\\AppData\\Roaming\\Python\\Python27\\site-packages',
'C:\\Anaconda2\\lib\\site-packages',
'C:\\Anaconda2\\lib\\site-packages\\win32',
'C:\\Anaconda2\\lib\\site-packages\\win32\\lib',
'C:\\Anaconda2\\lib\\site-packages\\Pythonwin',
'C:\\Anaconda2\\lib\\site-packages\\pywinpty-0.5-py2.7-win-amd64.egg']
Server time: Wed, 6 Jun 2018 07:48:44 +0000
Can anyone please tell me what's wrong present in the above code?

Instead of the following regex expression url(r"user_dashboard/NewRequest/([\{.*\}])", views.request_form, name = "NewRequest") in your URL:
try with re group name:
url(r"user_dashboard/NewRequest/(?P<device_name>\w.+)", views.request_form, name = "NewRequest")
Make sure you have device_name as a default argument in your view
def request_form(request,device_name=''):
'''

Related

NoReverseMatch at /auth/password/reset/

I am using dj-rest-auth package to do auth related functions, installed the package and added to installed apps and included it in main url as follows
path('auth/', include('dj_rest_auth.urls')),
it works for login, logout etc, but when I do password reset it throws the error
http://127.0.0.1:8000/auth/password/reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name
NoReverseMatch at /auth/password/reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Request Method: POST
Request URL: http://127.0.0.1:8000/auth/password/reset/
Django Version: 3.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Exception Location: /home/biju/Desktop/reporting-system/lib/python3.8/site-packages/django/urls/resolvers.py, line 694, in _reverse_with_prefix
Python Executable: /home/biju/Desktop/reporting-system/bin/python
Python Version: 3.8.10
Python Path:
['/home/biju/Desktop/reporting-system',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/biju/Desktop/reporting-system/lib/python3.8/site-packages']
Server time: Fri, 01 Apr 2022 16:01:18 +0000
the urlpattern in https://github.com/iMerica/dj-rest-auth/blob/master/dj_rest_auth/urls.py:
...
path('password/reset/confirm/', PasswordResetConfirmView.as_view(), name='rest_password_reset_confirm'),
...
so you need to change 'password_reset_confirm' to 'rest_password_reset_confirm' in your html url tag or in django function call like reverse()
update:
see https://dj-rest-auth.readthedocs.io/en/latest/faq.html
"You need to add password_reset_confirm url into your urls.py (at the top of any other included urls). Please check the urls.py module inside demo app example for more details."
the package expects something like:
path('password/reset/confirm/<uidb64>/<token>',TemplateView.as_view(template_name="password_reset_confirm.html"),
name='password_reset_confirm'),
path('auth/', include('dj_rest_auth.urls')),
see the demo urls.py: https://github.com/iMerica/dj-rest-auth/blob/24678437b3cdf3fa663880ab66a42aa0992b1d39/demo/demo/urls.py
strange for me that it is mentioned only in the FAQ - looks to me like a bug in the confirm email generation where they use the wrong urlpattern name without the "rest" prefix
.

Django Can't find my template

I've found many questions like this but no one have solved this mine.
When I visit the url
http://localhost:8000/catalog/book/88a26558-d636-44c8-8831-242d98fa6d80/renew/
that is handled by this urls.py path:
path('book/<uuid:pk>/renew/', views.renew_book_librarian, name='renew-book-librarian'),
than my view in the end returns this:
I have a view that returns this:
return render(request,'catalog/book_renew_librarian.html', {'form': form, 'bookinst':book_inst})
But I get the template error:
TemplateDoesNotExist at /catalog/book/88a26558-d636-44c8-8831-242d98fa6d80/renew/
catalog/book_renew_librarian.html
Request Method: GET
Request URL: http://localhost:8000/catalog/book/88a26558-d636-44c8-8831-242d98fa6d80/renew/
Django Version: 2.0.3
Exception Type: TemplateDoesNotExist
Exception Value:
catalog/book_renew_librarian.html
Exception Location: C:\Users\Araujo\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader.py in get_template, line 19
Python Executable: C:\Users\Araujo\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.4
Python Path:
['C:\\Users\\Araujo\\Desktop\\first_django\\locallibrary',
'C:\\Users\\Araujo\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
'C:\\Users\\Araujo\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\Araujo\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\Araujo\\AppData\\Local\\Programs\\Python\\Python36',
'C:\\Users\\Araujo\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
Server time: Wed, 14 Mar 2018 19:19:10 +0000
But I have the template in the
\catalog\templates\book_renew_librarian.html
How to solve it?
Your directory structure for the path you specified should be :
ProjectFolder
-projectfolder
-catalog
-templates
-catalog
book_renew_librarian.html
-manage.py

i have two projects in django i can not open admin site for my second project,but i can open for my first project,can any one help me

AttributeError at /admin/
'WSGIRequest' object has no attribute 'user'
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 2.0.2
Exception Type: AttributeError
Exception Value:
'WSGIRequest' object has no attribute 'user'
Exception Location: C:\Users\vaaz\Desktop\venv\lib\site-packages\django\contrib\admin\sites.py in has_permission, line 186
Python Executable: C:\Users\vaaz\Desktop\venv\Scripts\python.exe
Python Version: 3.4.3
Python Path:
['C:\Users\vaaz\website1',
'C:\Windows\system32\python34.zip',
'C:\Users\vaaz\Desktop\venv\DLLs',
'C:\Users\vaaz\Desktop\venv\lib',
'C:\Users\vaaz\Desktop\venv\Scripts',
'C:\Python34\Lib',
'C:\Python34\DLLs',
'C:\Users\vaaz\Desktop\venv',
'C:\Users\vaaz\Desktop\venv\lib\site-packages']
Server time: Mon, 12 Mar 2018 10:21:49 +0000
This looks similar to this post
with your limited error description I can only suggest a few things that were mentioned in the above post:
Older Django versions use MIDDLEWARE_CLASSES = (...) in the settings.py rather than MIDDLEWARE = (...) try changing that variable.
If you are using virtualenv be sure to activate it before trying to runserver
If you created your app in a different version of Django and then installed it on another version it might give you errors.

Getting 'DatabaseError' object has no attribute 'message' in DJango

I am trying to run the following code here to save information to the database. I have seen other messages - but - it appears that the solutions are for older versions of Python/DJango (as they do not seem to be working on the versions I am using now: Python 3.6.3 and DJango 1.11.7
if form.is_valid():
try:
item = form.save(commit=False)
item.tenantid = tenantid
item.save()
message = 'saving data was successful'
except DatabaseError as e:
message = 'Database Error: ' + str(e.message)
When doing so, I get an error message the error message listed below. How can I fix this so I can get the message found at the DB level printed out?
'DatabaseError' object has no attribute 'message'
Request Method:
POST
Request URL:
http://127.0.0.1:8000/storeowner/edit/
Django Version:
1.11.7
Exception Type:
AttributeError
Exception Value:
'DatabaseError' object has no attribute 'message'
Exception Location:
C:\WORK\AppPython\ContractorsClubSubModuleDEVELOP\libmstr\storeowner\views.py in edit_basic_info, line 40
Python Executable:
C:\WORK\Software\Python64bitv3.6\python.exe
Python Version:
3.6.3
Python Path:
['C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP',
'C:\\WORK\\Software\\OracleInstantClient64Bit\\instantclient_12_2',
'C:\\WORK\\Software\\Python64bitv3.6\\python36.zip',
'C:\\WORK\\Software\\Python64bitv3.6\\DLLs',
'C:\\WORK\\Software\\Python64bitv3.6\\lib',
'C:\\WORK\\Software\\Python64bitv3.6',
'C:\\Users\\dgmufasa\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP\\libintgr',
'C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP\\libmstr',
'C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP\\libtrans',
'C:\\WORK\\AppPython\\ContractorsClubBackofficeCode\\libintgr',
'C:\\WORK\\AppPython\\ContractorsClubBackofficeCode\\libmstr',
'C:\\WORK\\TRASH\\tempforcustomer\\tempforcustomer\\libtempmstr',
'C:\\WORK\\AppPython\\ContractorsClubBackofficeCode\\libtrans',
'C:\\WORK\\Software\\Python64bitv3.6\\lib\\site-packages',
'C:\\WORK\\Software\\Python64bitv3.6\\lib\\site-packages\\django-1.11.7-py3.6.egg',
'C:\\WORK\\Software\\Python64bitv3.6\\lib\\site-packages\\pytz-2017.3-py3.6.egg']
Server time:
Sat, 9 Dec 2017 08:42:49 +0000
Just change
str(e.message)
to
str(e)
some change
str(e.message)
to
HttpResponse(e.message)

Django: inlineformset widgets throwing error

Hi I have a inlineformset form, i need to change css class of its fields. I am using widgets to change the class.
However I am get error.
My form:
createinvoiceformset = inlineformset_factory(serviceinvoice, serviceinvoiceitems,can_delete=True,form=invoiceitemsform,extra=2,widgets={'Quantity': FloatField(attrs={'class':'quantity'})})
Error:
TypeError at /invoice/createinvoice/
__init__() got an unexpected keyword argument 'attrs'
Request Method: GET
Request URL: http://localhost:8000/invoice/createinvoice/
Django Version: 1.8.9
Exception Type: TypeError
Exception Value:
__init__() got an unexpected keyword argument 'attrs'
Exception Location: C:\Python34\lib\site-packages\django\forms\fields.py in __init__, line 245
Python Executable: C:\Python34\python.exe
Python Version: 3.4.0
Python Path:
['D:\\pythonspace\\tiktant',
'C:\\Python34\\lib\\site-packages\\django_select2-4.3.2-py3.4.egg',
'C:\\Python34\\lib\\site-packages\\python_dateutil-2.2-py3.4.egg',
'C:\\Python34\\lib\\site-packages\\babel-1.3-py3.4.egg',
'C:\\Python34\\lib\\site-packages\\django_classy_tags-0.5.1-py3.4.egg',
'C:\\Python34\\lib\\site-packages\\django_datetime_widget-0.9.3-py3.4.egg',
'C:\\Python34\\lib\\site-packages\\django_select2_py3-4.2.1-py3.4.egg',
'C:\\Python34\\lib\\site-packages\\django_bootstrap3-4.11.0-py3.4.egg',
'C:\\Windows\\system32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages']
Thank in advance for the help.
FloatField is not a widget. You should use NumberInput instead.