Hi i am creating a simple Sign Up form with django framework and mongodb. Following is my view:
class SignUpView(FormView):
template_name='MnCApp/signup.html'
form_class=EmployeeForm()
succes_url='/success/'
Following is my model:
class Employee(Document):
designation=StringField()
department=StringField()
emp_name=StringField(max_length=50)
password=StringField(max_length=10)
Following is my forms.py
class EmployeeForm(DocumentForm):
class meta:
desigs=(
('D','Director'),
('GM','General Manager'),
('AM','Assistant Manager'),
('A','Associates')
)
deptts=(
('HR','Human Resources'),
('IT','IT Support'),
('TT','Technical Team'),
('SM','Sales and Marketting'),
('SS','Support Staff')
)
document=Employee
fields='__all__'
widgets={
'designation':Select(choices=desigs),
'department':Select(choices=deptts)
}
Following is the traceback ValueError recieved on loading SignUpview
Traceback:
File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files\Python35\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" in get
174. return self.render_to_response(self.get_context_data())
File "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" in get_context_data
93. kwargs['form'] = self.get_form()
File "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" in get_form
45. return form_class(**self.get_form_kwargs())
File "C:\Program Files\Python35\lib\site-packages\mongodbforms\documents.py" in init
353. raise ValueError('A document class must be provided.')
Exception Type: ValueError at /signup/
Exception Value: A document class must be provided.
I am not able to find root of this problem. I am new to django and this is my first project. Also is their anyother way for creating model forms for mongo documents??
I suspect that your inner class should be called Meta, not meta.
Related
I'm trying to create a custom "list" under OptionViewSet but It's giving me an error.
class OptionViewSet(viewsets.ModelViewSet):
serializer_class = OptionSerializer
queryset = Option.objects.all()
def list(self, request):
queryset = Option.objects.all()
serializer = OptionSerializer(queryset, many=True)
return Response(serializer.data)
Error
__init__() takes 1 positional argument but 2 were given
This works fine:
class OptionViewSet(viewsets.ModelViewSet):
serializer_class = OptionSerializer
queryset = Option.objects.all()
urls.py
path('api/shipping/', include((shipping_routes.urls, 'shipping'), namespace='shipping')),
routes.py
routes.register(r'option', OptionViewSet, basename='option')
serializer
class OptionSerializer(serializers.ModelSerializer):
class Meta:
model = Option
fields = ['id', 'extra', 'name']
full traceback
Traceback (most recent call last):
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Simon\Documents\GitHub\crud-react-django\django_backend\shipping\views.py", line 18, in list
return Response(serializer.data)
Exception Type: TypeError at /api/shipping/option/
Exception Value: __init__() takes 1 positional argument but 2 were given
I had the wrong Response import. Imported it from "requests" instead of "rest_framework.response".
In the url.py, in the the specific path from urlpatters[] you need to call your views.OptionViewSet.as_view() as a middle parameter between the URL path and the name reference:
path('api/shipping/', include((shipping_routes.urls, 'shipping'), namespace='shipping')),
Django has very good documentation and you can read more about the Django-Rest-Framework's ViewSets here:
https://www.django-rest-framework.org/api-guide/viewsets/
edit: From the exception stack trace we can see that url.py is refering to a problem with /api/shipping/option/ which essentially is the routes.py's record with basename='option'
Seems like you might need to adjust routes.py from
routes.register(r'option', OptionViewSet, basename='option')
to
routes.register(r'option', OptionViewSet.as_view(), basename='option')
Can you please give it a go and let us know how it goes!
I'm trying to retrieve a model instance in Django Rest Framework 3.6.3 at /path/to/API/widget/1/, 1 being the primary key of the widget. It's giving me:
retrieve() got an unexpected keyword argument 'pk'
From the trace I think I'm doing something to cause dispatch to throw an exception when instantiating the handler (from views.py, source here). I don't understand what I'm doing wrong through when I examine the source. Here's the trace:
Traceback:
File "/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/lib/python3.5/site-packages/rest_framework/viewsets.py" in view
86. return self.dispatch(request, *args, **kwargs)
File "/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
489. response = self.handle_exception(exc)
File "/lib/python3.5/site-packages/rest_framework/views.py" in handle_exception
449. self.raise_uncaught_exception(exc)
File "/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
486. response = handler(request, *args, **kwargs)
Exception Type: TypeError at /path/to/API/widget/1/
Exception Value: retrieve() got an unexpected keyword argument 'pk'
Here's the view from the ViewSet:
def retrieve(self, request, some_lookup_field=None):
instance = self.get_object(pk) # handle multi-tenant access control
if not instance:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = self.get_serializer(instance) # from the class's `serializer_cass` attribute
return Response(serializer.data)
As you see in your stacktrace, the error is:
retrieve() got an unexpected keyword argument 'pk'.
Look closely in your retrieve() function:
def retrieve(self, request, some_lookup_field=None):
# ____________________________^
instance = self.get_object(pk) # handle multi-tenant access control
# _________________________^
...
You are taking some_lookup_field as a parameter and not pk, that you use below in self.get_object(pk). Try to rename some_lookup_field to pk.
This error might because you put wrong urls in your url.py. I've had the same problem in my project
I want to access ElasticSearch securely via a Django URL. From this request, I will lookup the user's token to get their primary key. Then I will only show ES results that match.
For now, I just want to connect to my ElasticSearch URL. It would kind of work like a "proxy".
Anyway, the following returns the bottom traceback. How do I set it up correctly? Do I need a serialiser
views.py
from django.http import HttpResponseRedirect
class ElasticViewSet(viewsets.ModelViewSet):
def my_view(request):
if True:
return HttpResponseRedirect('http://localhost:9200/_seach')
urls.py
router.register(r'elastic', ElasticViewSet, base_name='Elastic')
Traceback
Traceback:
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
83. return self.dispatch(request, *args, **kwargs)
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
477. response = self.handle_exception(exc)
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/rest_framework/views.py" in handle_exception
437. self.raise_uncaught_exception(exc)
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
474. response = handler(request, *args, **kwargs)
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/rest_framework/mixins.py" in list
40. queryset = self.filter_queryset(self.get_queryset())
File "/Users/mac1/Dev/A51/Backend/NewDJ/venv/lib/python2.7/site-packages/rest_framework/generics.py" in get_queryset
67. % self.__class__.__name__
Exception Type: AssertionError at /****/elastic/
Exception Value: 'ElasticViewSet' should either include a `queryset` attribute, or override the `get_queryset()` method.
First thing first, you don't want a ModelViewSet as there's no Model. Just go for the regular ViewSet.
Second, please take some time to think about what you want to do and how you want it done. my_view is not a ViewSet regular action. Returning HttpResponseRedirect is not acting as a proxy.
class CircularDetail(DeleteView):
model = Circular
template_name = 'genre/circular_detail.html'
def get_context_data(self, **kwargs):
ctx = super(CircularDetail, self).get_context_data(**kwargs)
ctx['c'] = Circular.objects.get(pk=self.pk)
ctx['sittings'] = Sitting.objects.all()
ctx['ballot'] = Sitting.objects.all()
return ctx
Above view give me following errors:
Traceback:
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/detail.py" in get
118. context = self.get_context_data(object=self.object)
File "/home/ohid/test_venv/myapp/genre/views.py" in get_context_data
126. ctx['c'] = Circular.objects.get(pk=self.pk)
Exception Type: AttributeError at /circular/1/detail/
Exception Value: 'CircularDetail' object has no attribute 'pk'
I need your assistance to fix this issues.
URL parameters are passed via kwargs, so you can access it this way
self.kwargs.get("pk")
so change
ctx['c'] = Circular.objects.get(pk=self.pk)
to
ctx['c'] = Circular.objects.get(pk=self.kwargs.get("pk"))
First of all, fix your base class to DetailView (instead of DeleteView)
DetailView inherited from django.views.generic.detail.SingleObjectMixin (https://docs.djangoproject.com/en/1.10/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin) which expects primary key to be in slug parameter, so fix your URL regexp to
r'^circular/(?P<slug>[-\w]+)/detail$'
and access your object using self.object in view or just {{ object }} in template.
I'm currently working on the following issue: The user can access the page test.com/BlogPostTitle. Where BlogPostTitle is a slug. If a Blog post with the fitting title exists, Django should render the DetailView of said blog post. If it doesn't exist, Django should render a form to create a blog post.
This works so far:
class EntryDetail(DetailView): # Displays blog entry, if it exists
model = Blog
slug_field = 'title'
template_name = 'app/entry.html'
class EntryForm(FormView): # Displays form, if entry 404s
template_name = 'app/create.html'
form_class = EntryForm
success_url = '/'
def form_valid(self, form):
form.save()
return super(EntryForm, self).form_valid(form)
class EntryDisplay(View):
def get(self, request, *args, **kwargs):
try:
view = EntryDetail.as_view()
return view(request, *args, **kwargs)
except Http404:
if check_user_editor(self.request.user) == True: # Fails here
view = EntryForm.as_view()
return view(request, *args, **kwargs)
else:
pass
Now, only users who are in the group "editor" should be able to see the form/create a post:
def check_user_editor(user):
if user:
return user.groups.filter(name="editor").exists() # Returns true, if user in editor group
else:
return Falseâ‹…
As you can see, I've implemented the function in the EntryDisplay, however, Django errors 'User' object is not iterable.
I'm guessing I've to work with SingleObjectMixin, but I haven't quite understood the docs on that.
Any help would be much appreciated.
Full traceback:
Traceback:
File "/home/django/local/lib/python3.4/site-packages/django/views/generic/detail.py" in get_object
53. obj = queryset.get()
File "/home/django/local/lib/python3.4/site-packages/django/db/models/query.py" in get
385. self.model._meta.object_name
During handling of the above exception (Blog matching query does not exist.), another exception occurred:
File "/home/django/mediwiki/mediwiki/views.py" in get
68. return view(request, *args, **kwargs)
File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/django/local/lib/python3.4/site-packages/django/views/generic/detail.py" in get
115. self.object = self.get_object()
File "/home/django/local/lib/python3.4/site-packages/django/views/generic/detail.py" in get_object
56. {'verbose_name': queryset.model._meta.verbose_name})
During handling of the above exception (No blog found matching the query), another exception occurred:
File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/django/mediwiki/mediwiki/views.py" in get
74. view = HttpResponse(request.user)
File "/home/django/local/lib/python3.4/site-packages/django/http/response.py" in __init__
293. self.content = content
File "/home/django/local/lib/python3.4/site-packages/django/http/response.py" in content
319. content = b''.join(self.make_bytes(chunk) for chunk in value)
File "/home/django/local/lib/python3.4/site-packages/django/utils/functional.py" in inner
235. return func(self._wrapped, *args)
Exception Type: TypeError at /test
Exception Value: 'User' object is not iterable
Your error is in line 74 in mediwiki.views:
view = HttpResponse(request.user)
HttpResponse expects a string or an iterable. Since request.user is not a string, it tries to use it as an iterable, which fails.
I can't say much without the actual code. If in fact you want to send just a string representation of the user as the response, you need to cast is to a string:
view = HttpResponse(str(request.user))
Is the error occurred at template rendering? If so I wonder you've make iter over attributes on single User object. I think you may need user.values().
BTW, check_user_editor should be simpler:
def check_user_editor(user):
return user.groups.filter(name="editor").exists()