codingforentrepreneurs / blog-api-with-django-rest-framework Goto Github PK
View Code? Open in Web Editor NEWBlog API with Django Rest Framework
License: MIT License
Blog API with Django Rest Framework
License: MIT License
Hi, I was following up on your tutorial Django + AngularJS for a Powerful Web Application where I get to edit /src/blog/urls.py I tried to import from django.views.genric.base import TemplateView
. When I cd
'd to src
folder and run python manage.py runserver
I got the following error:
ImportError: No module named genric.base
First of all, appreciate this project. This is very helpful. As I know tags feature is essential for a blog system. Admin should be able to control (add/edit/delete) tags and client user should be able to filter blogs via tags or searching.
models.py in posts app.
replace the exiting upload_location function with this code snippet. I have already ported my db to Postgres, hence I ain't making a pull request.
def upload_location(instance, filename):
#filebase, extension = filename.split(".")
#return "%s/%s.%s" %(instance.id, instance.id, extension)
PostModel = instance.__class__
last = PostModel.objects.order_by("id").last()
new_id = 1 if last == None else (last.id+1)
"""
instance.__class__ gets the model Post. We must use this method because the model is defined below.
Then create a queryset ordered by the "id"s of each object,
Then we get the last object in the queryset with `.last()`
Which will give us the most recently created Model instance
We add 1 to it, so we get what should be the same id as the the post we are creating.
"""
return "%s/%s" %(new_id, filename)
I see no relationship between the post and comment am i missing something here ?
We don't need to declare the field names of SerializerMethodField instances in read_only_fields
Hi, i am an audience from Youtube.When i was learning this course, i found some questions.
Blog-API-with-Django-Rest-Framework/src/accounts/api/views.py function:post
How can i sign out if it's needed.
self.request.user
.So the user logged in can log out.Thanks for the courses, i learned a lot from it.
Waiting for your replay.
When creating a post and there's no user logged in, the result shows "Authentication credentials were not provided", something like that.
When I'm trying to create a post and there's no user logged in, it doesn't show the message above. It still proceed in creating.
What do I need to do?
This the code the I used.
from rest_framework.generics import (
CreateAPIView,
DestroyAPIView,
ListAPIView,
UpdateAPIView,
RetrieveAPIView,
RetrieveUpdateAPIView
)
from rest_framework.permissions import (
AllowAny,
IsAuthenticated,
IsAdminUser,
IsAuthenticatedOrReadOnly,
)
from posts.models import Post
from .permissions import IsOwnerOrReadOnly
from .serializers import (
PostCreateUpdateSerializer,
PostDetailSerializer,
PostListSerializer
)
class PostCreateAPIView(CreateAPIView):
queryset = Post.objects.all()
serializer_class = PostCreateUpdateSerializer
permission_classes = [IsAuthenticated]
def perform_create(self, serializer):
serializer.save(user=self.request.user)
class PostDetailAPIView(RetrieveAPIView):
queryset = Post.objects.all()
serializer_class = PostDetailSerializer
lookup_field = 'slug'
#lookup_url_kwarg = "abc"
class PostUpdateAPIView(RetrieveUpdateAPIView):
queryset = Post.objects.all()
serializer_class = PostCreateUpdateSerializer
lookup_field = 'slug'
permission_classes = [IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
#lookup_url_kwarg = "abc"
def perform_update(self, serializer):
serializer.save(user=self.request.user)
#email send_email
class PostDeleteAPIView(DestroyAPIView):
queryset = Post.objects.all()
serializer_class = PostDetailSerializer
lookup_field = 'slug'
#lookup_url_kwarg = "abc"
class PostListAPIView(ListAPIView):
queryset = Post.objects.all()
serializer_class = PostListSerializer
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.