如何使基于类的视图在Django中工作

时间:2019-03-29 20:13:47

标签: python django python-3.x

我的项目是关于使用Django构建博客软件。应用程序中的所有内容都运行良好,但是我读到了有关基于类的视图的信息,由于我不熟悉OOP,因此无法完全理解。我只是想将我所有基于函数的视图转换为基于类的视图,但是当我尝试时,出现了此错误

  File "C:\Users\De Stone Of David\Desktop\python\second\projects\eblog\blog\views.py", line 146
    def(self, request, **kwargs):
       ^
SyntaxError: invalid syntax

这是基于函数的视图的代码 views.py

def newPost(request):    
    deyCat = Category.objects.all() 
    if request.method =='POST':
        myForm = NewPostForm(request.POST, request.FILES)
        response_data = {
            'SType': 'danger',
            'message': "An Error Occured, pls try again later"
        }
        if request.POST.get('deyHidden') == 'create_hidden':
            title = request.POST.get('title')
            content = request.POST.get('content')
            category_id = request.POST.get('category')
            image = request.FILES.get('image') 
            if myForm.is_valid():
                if Posts.objects.create(title=title, content=content, category_id=category_id, image=image, author_id=request.user.id):
                    response_data = {
                        'SType': 'success',
                        'message': "Saved Successfully"
                    }   
            return HttpResponse(json.dumps(response_data), content_type="application/json")  
        elif request.POST.get('deyHidden') == 'category_hidden':
            CatNames = request.POST.getlist('CatName[]')
            for CatName in CatNames:
                Category.objects.get_or_create(CatName=CatName)
            response_data = {
                'SType': 'success',
                'message': "Saved Successfully"
            }                
            return HttpResponse(json.dumps(response_data), content_type="application/json")

    context={
        'form':NewPostForm(),
        'title':'Create Post',
        'category': Category.objects.all()
    }
    return render(request, 'blog/form.html', context)

这是基于类的视图的代码 views.py

class NewPostView(TemplateView):
    template_name = 'blog/form.html'
    deyCat = Category.objects.all()

    def(self, request, **kwargs):
        context = {
            'form': NewPostForm(),
            'title': 'Create Post',
            'category': self.deyCat
        }
        return render(request, self.template_name, context)

    def post(self, request, **kwargs):
        myForm = NewPostForm(request.POST, request.FILES)
        response_data = {
            'SType': 'danger',
            'message': "An Error Occured, pls try again later"
        }
        if request.POST.get('deyHidden') == 'create_hidden':
            title = request.POST.get('title')
            content = request.POST.get('content')
            category_id = request.POST.get('category')
            image = request.FILES.get('image')
            if myForm.is_valid():
                if Posts.objects.create(title=title, content=content, category_id=category_id, image=image,
                                        author_id=request.user.id):
                    response_data = {
                        'SType': 'success',
                        'message': "Saved Successfully"
                    }
            return HttpResponse(json.dumps(response_data), content_type="application/json")
        elif request.POST.get('deyHidden') == 'category_hidden':
            CatNames = request.POST.getlist('CatName[]')
            for CatName in CatNames:
                Category.objects.get_or_create(CatName=CatName)
            response_data = {
                'SType': 'success',
                'message': "Saved Successfully"
            }
            return HttpResponse(json.dumps(response_data), content_type="application/json")

pls,如何解决此错误消息或我在做什么错呢?

1 个答案:

答案 0 :(得分:0)

在传递参数之前,应给函数命名。

您应该写def(self, request, **kwargs)而不是def my_function(self, request, **kwargs)