我正在尝试在Django中添加喜欢/不喜欢的功能而不刷新页面

时间:2020-03-14 16:36:36

标签: javascript python django ajax

我试图在不刷新页面的情况下添加喜欢/不喜欢的功能,但是我无法设置逻辑 这是我的models.py文件代码

from django.db import models

# Create your models here.


class EmployeeDetail(models.Model):
    emp_fname = models.CharField(max_length=50, default="")
    emp_lname = models.CharField(max_length=50, default="")
    emp_uname = models.CharField(max_length=100, default="")
    emp_email = models.EmailField(max_length=254, default="")
    emp_password = models.CharField(max_length=100, default="")
    emp_dob = models.DateField(max_length=50, default="")
    emp_doj = models.DateField(max_length=50, default="")
    emp_designation = models.CharField(max_length=100, default="")
    emp_salary = models.IntegerField()
    emp_leaves = models.IntegerField()
    emp_contact = models.CharField(max_length=12, default="")
    emp_photo = models.ImageField(upload_to="employee/images", default="")

    def __str__(self):
        return self.emp_fname


class Post(models.Model):
    emp_id = models.ForeignKey(EmployeeDetail, on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    slug = models.CharField(max_length=150)
    content = models.TextField()
    author = models.CharField(max_length=15)
    timeStamp = models.DateTimeField(auto_now_add=True)
    likes = models.IntegerField(default=0)
    dislikes = models.IntegerField(default=0)

    def __str__(self):
        return self.title + ' by ' + self.author


class Preference(models.Model):
    user_id = models.ForeignKey(EmployeeDetail, on_delete=models.CASCADE)
    post_id = models.ForeignKey(Post, on_delete=models.CASCADE)
    value = models.IntegerField()
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.user_id) + ':' + str(self.post_id) + ':' + str(self.value)

我的urls.py代码在这里吹

from django.urls import path
from .import views


urlpatterns = [
    path('', views.login, name='Login'),
    path('logout', views.logout, name='Logout'),
    path('employee/home/', views.home, name='Home'),
    path('employee/manageuser/', views.manageuser, name='manageuser'),
    path('employee/blank/', views.blank, name='blank'),
    path('employee/font_awesome/', views.font_awesome, name='fontawesome'),
    path('employee/map_google/', views.map_google, name='map google'),
    path('employee/not_found/', views.not_found, name='map google'),
    path('employee/register/', views.register, name='Registration'),
    path('employee/employee_profile/<int:id>', views.employee_profile, name='Employee Profile'),
    path('employee/delete_employee/<int:id>', views.delete_employee, name='Delete Employee'),
    path('employee/edit_employee/<int:id>', views.edit_employee, name='Edit Employee'),
    path('employee/create_post/', views.create_post, name='Create Post'),
    path('employee/blogpost/<slug:slug>', views.blogpost, name='Blog Post'),
    path('employee/blogpost/<int:post_id>/preference/userpreference', views.postpreference, name='postpreference')


]

我在这里看到了views.py文件代码

from django.shortcuts import render, redirect
from .models import EmployeeDetail, Post, Preference
from django.http import HttpResponse
from django.contrib import messages

# Create your views here.




def create_post(request):
    if request.session.has_key('emp_uname'):
        name = request.session['emp_uname']
        query = EmployeeDetail.objects.filter(emp_uname=name)

        if request.method == "POST":
            title = request.POST.get('title')
            slug = request.POST.get('slug')
            eid = request.POST.get('eid')
            euname = request.POST.get('ename')
            content = request.POST.get('content')
            empid = EmployeeDetail.objects.get(id=eid)


            post = Post(emp_id=empid, title=title, slug=slug, content=content, author=euname)
            # print(post.emp_id.id)
            post.save()
            return redirect('/employee/home')
        else:
            return render(request, 'create_post.html', {'query': query})
    else:
             messages.error(request, 'You have to login First')
             return redirect('/')


def blogpost(request, slug):
    if request.session.has_key('emp_uname'):
        name = request.session['emp_uname']
        query = EmployeeDetail.objects.filter(emp_uname=name)
        postslug = Post.objects.filter(slug=slug)
        return render(request, 'blogpost.html', {'postslug': postslug, 'query': query})
    else:
             messages.error(request, 'You have to login First')
             return redirect('/')


def postpreference(request, postid):
    return render(request, 'blogpost.html', context)

并且我在这里有blogpost.html文件,该页面中还包含特定的博客详细信息和“喜欢/不喜欢”链接,当有人喜欢/不喜欢这里的详细代码时,我不想刷新此页面

<div class="col-md-12 col-lg-8 col-sm-12">
                        <div class="white-box">
                            <h3 class="box-title">Latest Blog</h3>
                            <div class="comment-center p-t-10">
                                {% for fullpost in postslug %}
                                <div class="comment-body">
                                    <div class="user-img"> <img src="{% static 'plugins/images/users/pawandeep.jpg' %}" alt="user" class="img-circle">
                                    </div>
                                    <div class="mail-contnet">
                                        <h5>{{ fullpost.author }}</h5><span class="time">{{ fullpost.timeStamp }}</span>
                                        <h3><b>{{ fullpost.title}}</b></h3>
                                        <br/><span class="mail-desc">{{ fullpost.content }}</span>

                                        <a href="#" onClick="document.getElementById('likebutton').submit()">Like This Post</a> {{fullpost.likes}}   |
                                        <a href="#" onClick="document.getElementById('dislikebutton').submit()">Dislike This Post</a> {{fullpost.dislikes}}
                                    </div>
                                </div>
                                <!-- like form -->
                                <form id="likebutton" method="POST" action="/employee/blogpost/{{fullpost.id}}/preference/1">{% csrf_token %}
                                    <input type="hidden">
                                </form>
                                <!-- end like form -->

                                <!-- dislike form -->
                                <form id="dislikebutton" method="POST" action="/posts/{{fullpost.id}}/preference/2">{% csrf_token %}
                                <input type="hidden">
                                </form>
                                <!-- end dislike form -->
                                {% endfor %}
                            </div>
                        </div>
                    </div>

1 个答案:

答案 0 :(得分:0)