I'm trying to render some objects from my model to HTML. I initially thought this was going to be straightforward (and it likely is) but I'm coming up against numerous errors.
In this project I have built some API views that work fine (HintsListApiView and HintsRudView work). But I'd ideally like to use the API to produce a regular - read only HTML page that I can then style as I wish - my HTMLAPIView. I'm trying to use TemplateHTMLRenderer, but am coming up against errors. All I want is to get the text attribute to show up in HTML. The actual texts are just a sentence long each.
These are my files:
models.py:
from django.db import models
from rest_framework.reverse import reverse as api_reverse
class Hints(models.Model):
text = models.TextField(max_length=255)
author = models.CharField(max_length=20)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.text)
def timestamp_pretty(self):
return self.timestamp.strftime('%b %d %Y')
def get_api_url(self, request=None):
return api_reverse("api-hints1:hints-rud", kwargs={'pk': self.pk}, request=request)
views.py:
class HTMLAPIView(generics.RetrieveAPIView):
lookup_field = 'pk'
serializer_class = HTMLSerializer
renderer_classes = (TemplateHTMLRenderer,)
def get_queryset(self):
queryset = Hints.objects.value('text')
return Response({'queryset': queryset}, template_name='base.html')
class HintsListApiView(mixins.CreateModelMixin, generics.ListAPIView):
lookup_field = 'pk'
serializer_class = HintsSerializer
def get_queryset(self):
qs = Hints.objects.all()
query = self.request.GET.get("q")
if query is not None:
qs = qs.filter(
Q(text__icontains=query)|
Q(author__icontains=query)
).distinct()
return qs
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def get_serializer_context(self, *args, **kwargs):
return {"request": self.request}
class HintsRudView(generics.RetrieveUpdateDestroyAPIView):
lookup_field = 'pk'
serializer_class = HintsSerializer
def get_queryset(self):
return Hints.objects.all()
def get_serializer_context(self, *args, **kwargs):
return {"request": self.request}
serializers.py:
class HintsSerializer(serializers.ModelSerializer):
url = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Hints
fields = [
'url',
'id',
'text',
'author',
'timestamp_pretty'
]
read_only_fields = ['timestamp_pretty', 'id']
def get_url(self, obj):
request = self.context.get("request")
return obj.get_api_url(request=request)
class HTMLSerializer(serializers.ModelSerializer):
class Meta:
model = Hints
fields = [
'text',
]
read_only_fields = ['text',]
root urls.py:
from django.contrib import admin
from django.conf.urls import url, include
from rest_framework import routers, serializers, viewsets
urlpatterns = [
url('admin/', admin.site.urls),
url(r'^api/hints/', include(('hints1.api.urls', 'api'), namespace='api-hints1')),
url(r'^api-auth/', include('rest_framework.urls')),
]
urls.py:
from .views import HintsRudView, HintsListApiView, HTMLAPIView
from . import views
from django.contrib import admin
from django.conf.urls import url, include
from rest_framework import routers, serializers, viewsets
urlpatterns = [
url(r'^(?P<pk>\d+)$', HintsRudView.as_view(), name='hints-rud'),
url(r'^$', HintsListApiView.as_view(), name='hints-list'),
url(r'^html/', HTMLAPIView.as_view(), name='html' )
]
The errors I've faced were varied, currently I'm encountering AttributeError at /api/hints/html/
Manager object has no attribute 'value'.
I've tried with and without a serializer (because in the documentation it mentions TemplateHTMLRenderer doesn't need one). I think the problem lies within the view.py and the get_queryset function. I've tried various approaches but will get other errors like
TypeError context must be a dict rather than QuerySet.
Any help that can be provided will be greatly appreciated!
Thanks!
So I managed to do what I wanted. But to be honest I'm not completely sure why it worked, so if anyone can provide clarification, please do.
I changed my HTMLAPIView in views.py to a Viewset:
class HTMLAPIView(viewsets.ViewSet):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'base.html'
serializer_class = HTMLSerializer
def list(self, request):
queryset = Hints.objects.order_by('pk')
return Response({'queryset': queryset})
I then got an error in my urls.py but was able to fix that by including the dictionary portion in the .as_view()
url(r'^html/', HTMLAPIView.as_view({'get': 'list'}), name='html' )
The reason why I'm not sure why this worked is that it returns my text attribute from my model as I wanted but I don't see where I specified that that's the correct attribute. Is it just because it is the first one?
I also tried commenting out the serializer and it still worked fine, so I concluded that wasn't the reason for the results.
Related
I am writting an application in Django that uses the Django Rest framework. A GET request to the API URL works, but when I add a query to it, such as '?id=1845' it does not actually perform a query, it still returns the full list of results.
In the below code, I am trying to query the 'Indicator List'
views.py
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import IndicatorSerializer
from data_platform.models import Indicator
#api_view(['GET'])
def apiOverview(request):
api_urls = {
'Indicator List':'/indicator-list/',
'Indicator Detail':'/indicator-detail/<str:pk>/',
}
return Response(api_urls)
#api_view(['GET'])
def indicatorList(request):
indicators = Indicator.objects.all()
serializer = IndicatorSerializer(indicators, many=True)
#filterset_fields = ('id')
#filter_backends = [DjangoFilterBackend]
return Response(serializer.data)
#api_view(['GET'])
def indicatorDetail(request, pk):
indicators = Indicator.objects.get(id=pk)
serializer = IndicatorSerializer(indicators, many=False)
return Response(serializer.data)
urls.py
from django.urls import include, path
from . import views
urlpatterns = [
path('', views.apiOverview, name="api-overview"),
path('indicator-list/', views.indicatorList, name="indicator-list"),
path('indicator-detail/<str:pk>/', views.indicatorDetail, name="indicator-detail"),
]
serializers.py
from rest_framework import serializers
from data_platform.models import Indicator
class IndicatorSerializer(serializers.ModelSerializer):
class Meta:
model = Indicator
fields = '__all__'
I figured it out! I had to use a class view rather then a function view
class indicatorList(generics.ListAPIView):
queryset = Indicator.objects.all()
serializer_class = IndicatorSerializer
filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
filter_fields = ('id',)
and add 'django_filters', to INSTALLED_APPS
I am having a hard time to understand how to display function based views URLS with Django REST FRAMEWORK.
I have the setup below of my project but for some reason I am unable to display the endpoint while MovieListViewSet works.
PROJECT.URLS
from users import views
router = routers.DefaultRouter()
router.register(r'movies', MovieListViewSet)
urlpatterns = [
path('', include(router.urls)),
path('admin/', admin.site.urls),
path('profile/', views.ProfileList, name='ProfileList')
]
users.model
User = settings.AUTH_USER_MODEL
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500)
location = models.CharField(max_length=50)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return self.user.username
serializers
from rest_framework import serializers
from users.models import Profile
class ProfileSerializer(serializers.ModelSerializer):
user = serializers.StringRelatedField(read_only=True)
class Meta:
model = Profile
fields = (
'id',
'user',
#'bio',
#'location',
'image',
)
I have comment bio and location because when they are uncommented, I receive this message.
Got AttributeError when attempting to get a value for field `bio` on serializer `ProfileSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'bio'.
users.views (app)
#api_view(["GET"])
def ProfileList(APIView):
profile = Profile.objects.all()
serializer = ProfileSerializer(profile)
return Response(serializer.data)
I am unable to see ProfileList view as endpoint
Can someone point me to what I am doing wrong to display this endpoint to django rest framework.
You should specify many=True while serialization.
serializer = ProfileSerializer(profile, many=True)
You are mixing up functions and class based view definition here:
#api_view(["GET"])
def ProfileList(APIView):
profile = Profile.objects.all()
serializer = ProfileSerializer(profile)
return Response(serializer.data)
Either you define a class based view like this:
class ProfileView(APIView):
profile = Profile.objects.all()
serializer = ProfileSerializer
def list(request):
return Response(self.serializer_class(self.get_queryset(), many=True).data)
Or a function base view like:
#api_view(["GET])
def profile_list(request):
return Response(ProfileSerializer(Profile.objects.all(), many=True).data)
I have a Django Project where i made 3 Different Apps: "blog", "users", "api".
It is a Website where messages can be posted by using a model Post. I want to use an Django Rest API for accessing the Model. It works, but it messes with some redirects of the UpdateView and DeleteView of "blog".
I think it could be a problem with using DefaultRouter() ?
When i try to use my blog/PostupdateView blog/PostDeleteView ( inherited from UpdateView and DeleteView) views, i keep getting redirected to /api/blog/postid/ instead of just accessing my detailView where the path should be just /blog/postid/
and i cannot figure out why.
my Post Model:
class Post(models.Model):
...
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
my Serializer:
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('id', 'title', 'content', 'date_posted', 'author', 'rooms')
my api view for Post:
class PostView(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
My urls Files:
main urls.py:
urlpatterns = [
...
path('', include('blog.urls')),
path('api/',include('api.urls')),
]
blog/urls.py:
urlpatterns = [
...
path('post/<int:pk>/', PostDetailView.as_view(),name='post-detail'),
path('post/new/', PostCreateView.as_view(),name='post-create'),
...
]
api/urls.py:
router = routers.DefaultRouter()
router.register('post', views.PostView)
urlpatterns = [
path('',include(router.urls))
]
my PostCreateView in blog/views.py
class PostCreateView( LoginRequiredMixin, UserPassesTestMixin, CreateView):
model = Post
fields = ['title', 'content', 'rooms']
def test_func(self):
...
def get_form(self, form_class=None):
...
def form_valid(self, form):
...
When using PostCreateView, i should be redirected to the detail-view of the created Post, as defined in the Post model. Instead im getting redirected to the api url that is generated by the router in api/urls.py
One of the easy methods to solve the issue is, change the get_absolute_url() method as
class Post(models.Model):
...
def __str__(self):
return self.title
def get_absolute_url(self):
return "/blog/{}/".format(self.pk)
UPDATE
What was the problem?
You are defined the URLs with the same name, post-detail, for API and usual view. You should name the URLs with unique names.
# blog/urls.py
urlpatterns = [
...
path('post/<int:pk>/', PostDetailView.as_view(), name='blog-post-detail'),
path('post/new/', PostCreateView.as_view(), name='blog-post-create'),
...
]
#models.py
class Post(models.Model):
...
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog-post-detail', kwargs={'pk': self.pk})
I am new and can't comment, so I'll answer OP's question in the comment on the accepted answer, for anyone else who find this.
When you register a route with the default router, Django will automatically assign a set of url's for them, which are in the format {basename}-{something else}, where {something else} depends on what action you called and the HTTP method. The basename is determined by the queryset attribute of the viewset (so your basename would be post).
For "GET" + "retrieve" action, the url will be {basename}-detail, which for your queryset translated to post-detail, which overrides the post-detail you defined for the non-api view.
You can get around it by registering the router with a basename, so the non-api post-detail URL name will correspond to the post/<pk> URL, and {other basename}-detail can correspond to the API view:
router.register('post', views.PostView, basename='api-post')
https://www.django-rest-framework.org/api-guide/routers/
I am new to Django, I was wondering How to code PUT request and Delete method in my Django REST API Project.
I have tried to follow with many tutorials, but none working with my kind of code.
The code I tried looks like this
def put(self, request, pk):
snippet = self.get_object(pk)
serializer = UserSerializers(snippet, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk):
snippet = self.get_object(pk)
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
But getting message method not allowed
The following is my Models.py
from django.db import models
class Users(models.Model):
user_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
profile_name = models.CharField(max_length=30)
profile_picture = models.CharField(max_length=1000)
phone = models.CharField(max_length=15)
email = models.EmailField()
password = models.CharField(max_length=25, default='')
def __str__(self):
return self.name
The following is my serializers.py
from rest_framework import serializers
from .models import Users
from .models import UserPost
class UserSerializers(serializers.ModelSerializer):
class Meta:
model = Users
fields = '__all__'
class UserDetailSerializer(serializers.ModelSerializer):
class Meta:
model = Users
fields = '__all__'
class UserPostSerializers(serializers.ModelSerializer):
class Meta:
model = UserPost
fields = '__all__'
The following code contains my Views.py
from django.http import Http404
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from .models import Users
from .models import UserPost
from .serializers import UserSerializers, UserPostSerializers
class UsersList(APIView):
def get(self, request):
User_details = Users.objects.all()
serializer = UserSerializers(User_details, many=True)
return Response(serializer.data)
def post(self, request):
serializer = UserSerializers(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class UserDetail(APIView):
def get_object(self, pk):
try:
return Users.objects.get(pk=pk)
except Users.DoesNotExist:
raise Http404
def get(self, request, pk):
snippet = self.get_object(pk)
serializer = UserSerializers(snippet)
return Response(serializer.data)
class UserViewSet(viewsets.ModelViewSet):
queryset = Users.objects.all()
serializer_class = UserSerializer
#action(methods=['put'], detail=False)
def filter_by_something(self, request):
something = request.data.get('that_something')
queryset = Users.objects.filter(something__exact=something)
return Response(self.get_serializer(queryset, many=True).data)
The following code contains urls.py
from sys import path
from django.urls import include
from rest_framework.urlpatterns import format_suffix_patterns
from app import views
from django.conf.urls import url
from django.contrib import admin
from rest_framework import routers
router = routers.DefaultRouter()
router.register('user_details', UserDetailViewSet)
urlpatterns = [path('api/', include(router.urls)), ...]
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^User_details/', views.UsersList.as_view()),
url(r'^User_details/(?P<pk>[0-9]+)/', views.UserDetail.as_view()),
url(r'^userpost_details/', views.UsersPostList.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)
Define put & delete methods in UsersDetail class.
class UsersDetail(APIView):
def put(self, request, pk):
// code
def delete(self, request, pk):
//code
If the above does not work also add http_method_names
class UsersDetail(APIView):
http_method_names = ['GET', 'PUT', 'DELETE']
class APIView inherits from class View defined in the base file of Django. dispatch method will check http_method_names for valid method. PUT and DELETE is listed as valid methods along with others.
Reference: django/views/generic/base.py
class View(object):
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)
def http_method_not_allowed(self, request, *args, **kwargs):
logger.warning('Method Not Allowed (%s): %s', request.method, request.path,
extra={
'status_code': 405,
'request': request
}
)
return http.HttpResponseNotAllowed(self._allowed_methods())
In urls.py you should declare a router object that maps your ViewSets to DRF:
from rest_framework import routers
router = routers.DefaultRouter()
router.register('user_details', UserDetailViewSet)
urlpatterns = [path('api/', include(router.urls)), ...]
UserDetailViewSet must be declared in api\views.py (api is the directory for DRF application)
from rest_framework.decorators import action
from rest_framework import viewsets
from rest_framework.response import Response
class UserDetailViewSet(viewsets.ModelViewSet):
queryset = UserDetail.objects.all()
serializer_class = UserDetailSerializer
#action(methods=['put'], detail=False)
def filter_by_something(self, request):
something = request.data.get('that_something')
queryset = UserDetail.objects.filter(something__exact=something)
return Response(self.get_serializer(queryset, many=True).data)
Then you will make PUT requests on http:\\your_website\api\user_details\filter_by_something
Try something like this. It should work!
I got an error,
AttributeError at /app/api/get
Got AttributeError when attempting to get a value for field task_name on serializer TaskSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the Color instance.
Original exception text was: 'Color' object has no attribute 'task_name'.
Now I wanna make a page that shows model's content in json format.
models.py is
from django.db import models
# Create your models here.
class Color(models.Model):
name = models.CharField(max_length=255)
background_color = models.CharField(max_length=255)
h1_color = models.CharField(max_length=255)
p_color = models.CharField(max_length=255)
def __str__(self):
return self.name
serializers.py is
from .models import Color
from rest_framework import serializers
class TaskSerializer(serializers.Serializer):
task_name = serializers.CharField(max_length=100)
status = serializers.SerializerMethodField('get_task_status')
def get_task_status(self, instance):
return instance.status.status
class Meta:
model = Color
fields = ('name',
'background_color',
'h1_color',
'p_color',
'task_name')
urls.py is
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'api/get',views.TaskGet.as_view(),name='task-get')
]
views.py is
from django.shortcuts import render
from .models import Color
from .forms import ColorForm
from .serializers import TaskSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
# Create your views here.
def index(request):
d = {
'colors': Color.objects.all(),
'form': ColorForm(),
}
return render(request, 'index.html', d)
class TaskGet(APIView):
def get(self, request, format=None):
obj = Color.objects.all()
serializers = TaskSerializer(obj, many=True)
return Response(serializers.data, status.HTTP_200_OK)
I wrote url(r'api/get',views.TaskGet.as_view(),name='task-get') in urls.py,so I really cannot understand why this error happens.I already run commands of migration of model. How can I fix this?
My ideal web page is like
You try get status by foreign key instance.status.status but in your model class Color i don't see any foreign keys or methods for it.
And for task_name did you want to see the model field name try to add source params
task_name = serializers.CharField(max_length=100, source='name')
# ^^^^^^^^^
are you sure you want serialize Task for model Color?
new edit
in your get_task_status the 'instanceis instance of serializer model, so if your modelColorhas no property or methodstatus` you will catch an error