如何显示属于exacly一类的记录

时间:2018-02-07 15:11:09

标签: python django

我创造了这样的东西 enter image description here

在标签'监控www'记录现在正确显示。如果用户已登录,他们可以添加网站,之后用户可以添加关键字。现在我想显示网站和属于网站记录的关键字。

view.py 我有:

@login_required
def website_list(request):
    website_list_user = Website.objects.filter(user=request.user).order_by('-user')
    context = {'website_list_user': website_list_user}
    return render(request, 'konto/dashboard.html', context=context)

@login_required
def keyword_list(request):
    keyword_list_user = Keyword.objects.filter(keyword=request.keyword).order_by('-keyword')
    context = {'keyword_list_user': keyword_list_user}
    return render(request, 'konto/dashboard.html', context=context)
forms.py

中的

class KeywordForm(forms.ModelForm):
    class Meta:
        model = Keyword
        fields = ('www', 'keyword')

    def __init__(self, user, *args, **kwargs):            
        super(KeywordForm, self).__init__(*args, **kwargs)
        self.fields['www'].queryset = user.website_set.all() 
        self.user = user

    def save(self, commit=True):
        instance = super(KeywordForm, self).save(commit=False)
        instance.user = self.user
        if commit:
            instance.save()
        return instance

和我的模板

{% if request.user.is_authenticated %}
<b>Monitoring www:</b><br>
{% for website in website_list_user %}
        {{website.website}}<br> 
{% endfor %}
<b>Słowa kluczowe:</b><br>
{% for keyword in keyword_list %}
{{keyword.keyword}}<br>
{% endfor %}
{% endif %}

编辑:

我的 urls.py

from django.urls import path
from django.contrib.auth import views as auth_views
from .views import dashboard, register, settings, del_user, website_list, keyword_list, new_website, new_keyword, main_page
urlpatterns =[
    path('', main_page, name='main_page'),
    path('login/', auth_views.login, name='login'),
    path('logout/', auth_views.logout, name='logout'),
    path('logout-then-login/', auth_views.logout_then_login, name='logout_then_login'),
    path('password-change/', auth_views.password_change, name='password_change'),
    path('password-change-done/', auth_views.password_change_done, name='password_change_done'),
    path('register/', register, name="register" ),
    path('settings/', settings, name='settings'),
    path('del-user/<slug:username>', del_user, name='del_user'),
    path('konto/', website_list, name='website_list'),
    path('konto/', keyword_list, name='keyword_list'),
    path('new-website/', new_website, name='new_website'),
    path('new-keyword/', new_keyword, name='new_keyword'),
    path('', dashboard, name='dashboard'),
]

我的 models.py

from django.db import models
from django.contrib.auth.models import User

class Website(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Użytkownik")
    website = models.CharField(max_length=250,verbose_name='Strona www', unique=False)

    class Meta:
        verbose_name = 'Strona www'
        verbose_name_plural = 'Strony www'

    def __str__(self):
        return self.website

class Keyword(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Użytkownik")
    www = models.ForeignKey(Website, on_delete=models.CASCADE, verbose_name="Strona www")
    keyword = models.CharField(max_length=250, verbose_name="Słowo kluczowe", unique=False)

    class Meta:
        verbose_name = 'Słowo kluczowe'
        verbose_name_plural = 'Słowa kluczowe'

    def __str__(self):
        return self.keyword

1 个答案:

答案 0 :(得分:1)

试试这个

{% for website in website_list_user %}
        {{website.website}}<br> 
        {% for keyword in website.keyword_set.all %}
           {{keyword.keyword}}
        {% endfor %}
{% endfor %}

或者修复模板

 {% for keyword in keyword_list_user %}
{{keyword.keyword}}<br>
{% endfor %}