我按照django休息框架教程(http://192.241.153.25:8000)
创建了一个网站我完成了登录部分和帖子的连接, 但我不清楚如何为个别帖子张贴发布表格和评论框。
这是我尝试过的事情
HTML
<form id="post_form" method="post" action="">
<input type="text" name="title" placeholder="Username">
<input type="text" name="content" placeholder="content">
<button class="ladda-button button-primary login_button" ng-click="vm.submit()"/><span class="ladda-label">submit</span></button>
</form>
jquery的
$('#post_form').on('submit', function(e) { e.preventDefault()
$.ajax({
type:"POST",
url: '/api/posts/create/',
data:$('#post_form').serialize(),
error: function(response){
alert('Not authorized.'); // Or something in a message DIV
},
success: function(response){
console.log(response);
$('.login_bar').html(response)
$('.login_bar').html(response)
$('#logout_form').toggleClass(show)
// do something with response
}
});
});
这是我的API。
http://192.241.153.25:8000/api/posts/create/
有人可以帮助我吗?
答案 0 :(得分:1)
看看这个例子:
models.py
class Post(models.Model):
title = models.CharField(max_length=500)
body = models.TextField()
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post)
text = models.TextField()
author = models.CharField(max_length=100)
def __str__(self):
return "[%s] %s" % (self.author, self.text)
serializers.py
from .models import Post, Comment
from rest_framework import serializers
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ('text', 'author', 'post')
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('title', 'body', 'comment_set')
views.py
from rest_framework import viewsets
from rest_framework.permissions import AllowAny
from .models import Post, Comment
from .serializers import CommentSerializer, PostSerializer
class PostViewSet(viewsets.ModelViewSet):
serializer_class = PostSerializer
permission_classes = [AllowAny]
queryset = Post.objects.all()
class CommentViewSet(viewsets.ModelViewSet):
serializer_class = CommentSerializer
permission_classes = [AllowAny]
queryset = Comment.objects.all()
urls.py
from django.conf.urls import url, include
from rest_framework import routers
from .views import PostViewSet, CommentViewSet
router = routers.DefaultRouter()
router.register(r'posts', PostViewSet)
router.register(r'comments', CommentViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
使用以下内容将JSON格式的POST请求发送到http://127.0.0.1/comments/
:
{"author":"Jerry", "text": "Nice post!", "post": 1}
为帖子创建一个新评论,在这种情况下为id = 1
答案 1 :(得分:0)
这可能适合您: views.py: -
"use strict"
forms.py
def adComment(request):
print request
if request.method == 'POST':
form = CommentForm(request.POST) #Validate your data with form.
if form.is_valid():
instance = form.save(commit=False)
instance.title = form.cleaned_data['username']
instance.comments = form.cleaned_data['content']
instance.save() #Will save your data if valid.
data = {'success':True,'msg':"Store comment's data successfully"}
return Response(data)
else:
data = {'success':False,'msg':'Error in storing comments'}
return Response(data)