34👍
html_from_view = ExampleDetail.as_view({'get': 'list'})(request).content
OR
html_from_view = ExampleDetail.as_view({'get': 'retrieve'})(request, pk=my_id).render().content
41👍
I found the solution for this in the documentation… https://docs.djangoproject.com/en/4.1/ref/class-based-views/mixins/
Hint is from their example here:
class AuthorDetail(View):
def get(self, request, *args, **kwargs):
view = AuthorDisplay.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = AuthorInterest.as_view()
return view(request, *args, **kwargs)
- [Django]-How to override and extend basic Django admin templates?
- [Django]-Django Migrations Add Field with Default as Function of Model
- [Django]-How do you insert a template into another template?
20👍
As of Django 2.2 and DRF 3.9.2 I am able to get response using below code.
response = UserItemsApiView.as_view()(request=request._request).data
Above example solves below issues:
- The
request
argument must be an instance ofdjango.http.HttpRequest
, notrest_framework.request.Request
- Instead of
content
, usingdata
attribute gave me result from that view.
- [Django]-How do I print out the contents of my settings in a django shell?
- [Django]-Django Admin – Disable the 'Add' action for a specific model
- [Django]-Django set field value after a form is initialized
1👍
The syntax of some of the answers was confusing to me, even though that’s how the django docs explain it:
So here’s an answer that’s a little more understandable for my small brain:
class MyTemplateView(TemplateView):
template_name = 'my_template.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
view = MyApiView.as_view()
response = view(request=self.request)
data = response.data
context["somedata"] = data
return context
- [Django]-TextField missing in django.forms
- [Django]-Celery – Get task id for current task
- [Django]-How to force Django models to be released from memory
-10👍
If I’m understanding correctly, you need to get the result from view B, while inside view A.
Using the requests/urllib2 and json libraries should solve your problem (as specified in this answer).
To get the URL, you can use a combination of request.get_absolute_uri() and/or request.get_host() and django.core.urlresolvers.reverse.
- [Django]-How to create user from django shell
- [Django]-How to expire session due to inactivity in Django?
- [Django]-Using django-rest-interface