簡體   English   中英

如何固定 django 中的帖子?

[英]How to pin posts in django?

所以我想知道如何在 django 中固定帖子。 當我單擊檢查並單擊創建帖子時,它應該將帖子固定到頂部。 較新的帖子只會出現在底部,而不會干擾固定的帖子。 固定系統還應該能夠固定多個帖子。 因此,如果我固定另一個帖子,則兩個帖子都應保持在頂部。 其他固定的帖子應顯示在下方。

models.py

class AskSection(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    is_pin = models.BooleanField()
    likes = models.ManyToManyField(User,  related_name='likes', blank=True) 
    is_marked = models.BooleanField(default=False)
    date_posted = models.DateTimeField(default=timezone.now)

    class Meta:
        ordering = ['-date_posted']
        verbose_name_plural = "Ask Section"

    def __str__(self):
        return str(self.title)

forms.py

class AskSectionCreateForm(forms.ModelForm):
    is_pin = forms.BooleanField(label="pin ask post", required=False)

    class Meta:
        model = AskSection
        fields = ('title', 'description', 'is_pin')

        widgets = {
            'title': forms.TextInput(attrs={
                'class': 'form-control'
            }),
            'description': forms.Textarea(attrs={
                'class': 'form-control'
            }),
        }

views.py

@login_required
def create_ask_post(request):
    if request.method == "POST":
        form = AskSectionCreateForm(request.POST or None)
        if form.is_valid():
            title = form.cleaned_data.get('title')
            description = form.cleaned_data.get('description')
            is_pin = form.cleaned_data.get('is_pin')

            obj = form.save(commit=False)
            obj.title = title
            obj.description = description
            obj.is_pin = is_pin
            obj.user = request.user
            obj.save()

            messages.success(request, f'You have posted {title} successfully')
            return redirect('/details_ask/' + str(title) + '/')
        else:
            form = AskSectionCreateForm()
    else:
        form = AskSectionCreateForm()
    
    context = {
        'form': form
    }
    return render(request, "editor/create_ask_post.html", context)

html file

{% for question in all_questions %}
   <!-- random HTML not important code --> <!-- code is related to just the styling of posts and just model fields -->
{% endfor %}

所以請讓我知道如何做到這一點。 HTML 文件並不重要。 它只包含卡和 model 字段。

謝謝!

因此,您實際上並不需要 model 中的另一個字段,您可以使用 DateTimeField,但正如在我將rank = models.Integerfield(default=0)添加到AskSection model 之前所評論的那樣。 (別忘了遷移)

您有一個帶有 function 的 views.py 文件,您可以在其中設置 html 的上下文(不是您在答案中顯示的那個,而是您定義all_questions的另一個)。 在這里,您可以為 all_questions 設置順序,如下所示all_questions = AskSection.objects.filter(user=request.user).order_by("rank", "-is_pin") 你的代碼現在可能看起來有點不同我不知道你是否按用戶過濾我只是假設......

當用戶添加新問題時,您會增加您在新問題上的排名,因此您始終擁有干凈的訂單。 每當用戶“固定”一個問題時,您將獲得最高排名並添加一個數字 + 將Boolean設置為True

另一種方法是使用 Datefield date_posted ,就像all_questions = AskSection.objects.filter(user=request.user).order_by("date_posted", "-is_pin") 在這種情況下,日期將充當“等級”。 為您節省了遷移,但它不如 Integerfield 靈活。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM