jquery 如何只使用js获取用户id或指定用户

txu3uszq  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(119)
class Generalnewssortview(View):
def post(self,request):
    user=request.user
    if request.method == 'POST':
        search_query = request.POST.get('search_query')
        sort_by = request.POST.get('sort_by')
        sort_by2 = request.POST.get('sort_by2')
        user_by = request.POST.get('user_by')
        today = datetime.today()
        year = today.year
        month = today.month
        day = today.day
        print('gen_news user ',user_by)
        print('gen_news sort1 ',sort_by)
        print('gen_news sort2 ',sort_by2)

        # both deos not have values
        if sort_by=='' and sort_by2=='':

            if user_by=='All':    
                data = news_updates.objects.filter(news_title__icontains=search_query,news_created__day=day,news_created__month=month,news_created__year=year,news_catagory='General').order_by('-news_created')
                response_data = []
                for item in data:
                    # print(item.news_created)
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
            else:
                data = news_updates.objects.filter(news_title__icontains=search_query,user=user_by,news_created__day=day,news_created__month=month,news_created__year=year,news_catagory='General').order_by('-news_created')
                response_data = []
                for item in data:
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
        # both have values
        elif sort_by!='' and sort_by2!='':
            if user_by=='All':
                data = news_updates.objects.filter(news_title__icontains=search_query,news_created__gte=(sort_by),news_created__lte=(sort_by2),news_catagory='General').order_by('-news_created')
                # print(data)
                response_data = []
                for item in data:
                    print(item.news_created)
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
            else:
                data = news_updates.objects.filter(news_title__icontains=search_query,user=user_by,news_created__gte=(sort_by),news_created__lte=(sort_by2),news_catagory='General').order_by('-news_created')
                response_data = []
                for item in data:
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
        # starting date have value
        elif sort_by!='' and sort_by2=='':
            if user_by=='All':
                data = news_updates.objects.filter(news_title__icontains=search_query,news_created__gte=(sort_by),news_catagory='General').order_by('-news_created')
                # print(data)
                response_data = []
                for item in data:
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
            else:
                data = news_updates.objects.filter(news_title__icontains=search_query,user=user_by,news_created__gte=(sort_by),news_catagory='General').order_by('-news_created')
                response_data = []
                for item in data:
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
        elif sort_by=='' and sort_by2!='':
            if user_by=='All':
                data = news_updates.objects.filter(news_title__icontains=search_query,news_created__lte=(sort_by2),news_catagory='General').order_by('-news_created')
                # print(data)
                response_data = []
                for item in data:
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
            else:
                data = news_updates.objects.filter(news_title__icontains=search_query,user=user_by,news_created__lte=(sort_by2),news_catagory='General').order_by('-news_created')
                response_data = []
                for item in data:
                    response_data.append({
                    'image_url': item.news_img.url,
                    'news_title': item.news_title,
                    'news_desc': item.news_desc,
                    'news_created':item.news_created,
                    })
        return JsonResponse(response_data, safe=False)

字符串

function fetchData(searchQuery, sortBy,sortBy2,userBy) {
    $.ajax({
        url: '/search_sort_data/',
        type: 'POST',
        headers: {
            "X-CSRFToken": getCookie("csrftoken")
          },
        data: {
            'search_query': searchQuery,
            'sort_by': sortBy,
            'sort_by2':sortBy2,
            'user_by':userBy
        },
        success: function(data) {
            var cardsContainer = $('#news-container');
            cardsContainer.empty();
            
            $.each(data, function(index, item) {
                var cardHtml = `
                    <div class="col">
                        <div class="card">
                            <img src="${item.image_url}" class="card-img-top" alt="News">
                            <div class="card-body">
                                <h5 class="card-title"> ${item.news_title}</h5>
                                <p class="card-text">${item.news_created}</p>
                                <p class="card-text">${item.news_desc}</p>
                            </div>
                        </div>
                    </div>
                `;
                cardsContainer.append(cardHtml);
            });
            if(data==''){
                cardsContainer.append(`<h5>No news to display on this day</h5>`);
            }
        }
    });
}

    fetchData('', '','','All');

$('#search-btn').click(function() {
    var searchQuery = $('#search-input').val();
    var sortBy = $('#date-input').val();
    var sortBy2 = $('#date-input2').val();
    var userBy = $('#user-select').val();
    fetchData(searchQuery,sortBy,sortBy2,userBy);
})

$('#user-select').change(function() {
    var searchQuery = $('#search-input').val();
    var sortBy = $('#date-input').val();
    var sortBy2 = $('#date-input2').val();
    var userBy = $(this).val();
    fetchData(searchQuery,sortBy,sortBy2,userBy);
});

$('#date_submit').click(function() {
    var searchQuery = $('#search-input').val();
    var sortBy = $('#date-input').val();
    var sortBy2 = $('#date-input2').val();
    var userBy = $('#user-select').val();
    fetchData(searchQuery,sortBy,sortBy2,userBy);
});


我正在用Django做一个项目。在我的新闻页面,它显示了所有用户上传的新闻,我需要的是把每个新闻下的删除按钮,如果由登录用户上传,而不是每一个新闻。(即,登录用户只能删除他上传的新闻)需要一个条件来检查这一点。这个问题可能很愚蠢,但是我希望能用js来实现,上面的代码片段是我如何获取新闻的,“顺便说一下,我是django的新手;)”
News page looks somewhat like this

3yhwsihp

3yhwsihp1#

你不需要js来做,你可以用HTML来做。假设您的News模型具有User外键。在HTML中,你可以渲染所有的新闻,你可以为删除按钮设置一个条件,像这样:

{% if user.is_authenticated and request.user.id == your_queryset_variable_name.user_id %}
<button> Delete </button>
{% endif %}

字符串

相关问题