繁体   English   中英

对不同的网页使用相同的模型

[英]Using same models for different webpages

所以我有一个主题。

class Topic(models.Model):
    topic_choices = (
                    ('t_topic', 't_topic',),
                    ('f_topic', 'f_topic',)
    )
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE) 
    type = models.CharField(max_length=100, choices=topic_choices)

    def __str__(self):
        return self.text 

将根据创建日期检索并显示该主题。

def topics(request):
    """Show all topics """
    topics = Topic.objects.order_by('date_added')
    context = {'topics': topics}
    return render(request, 'learning_logs/topics.html', context)

我想做的就是根据创建主题的方式来区分主题。

然后,我希望同一主题显示在同一网页上。

如您所见,在下面。 因为使用了相同的topic_id,所以同一主题显示在topic.html和f_topic.html上。

我想要它,以便如果该主题是在topic.html上创建的,那么它将显示在topic.html上。 如果是在f_topic.html上创建的,那么它将显示在f_topic.html上。

def topic(request, topic_id, type):
    topic = Topic.objects.get(id=topic_id, type='t_topic')
    entries = topic.entry_set.order_by('-date_added')
    images = Image.objects.filter(imgtopic__in=entries)
    context = {'topic': topic, 'entries': entries, 'images': images}
    return render(request, 'learning_logs/topic.html', context)

def f_topic(request, topic_id):
    topic = Topic.objects.get(id = topic_id)
    entries = topic.entry_set.order_by('-date_added')
    images = Image.objects.filter(imgtopic__in = entries)
    context = {'topic': topic, 'entries': entries, 'images': images}
    return render(request, 'learning_logs/f_topic.html', context)

主题如何另存为t_type

def new_topic(request):

    if request.method != 'POST':
    #No data submitted; create a blank form.
    form = TopicForm()

        if form.is_valid():
            new_topic = form.save(commit = False)
            new_topic.type = 't_topic'
            new_topic.owner = request.user 
            new_topic.save() 
            form.save()
            return HttpResponseRedirect(reverse('learning_logs:topics'))
    else:
        form = TopicForm(request.POST)

    context = {'form': form}
    return render(request, 'learning_logs/new_topic.html', context)'

topic.html

 {% extends 'learning_logs/base.html' %}

 {% block content %}
    <div class = 'topic-heading'>
     <p>TOPIC : {{topic}}</p>
    </div>

    <div class = 'topic-container'> 
     {%include 'learning_logs/text.html'%}
    </div>

我在/ topics / Reverse遇到与主题'(10,'')'相同的'topic'的NoReverseMatch错误。 尝试了1个模式:['topics /(?P [0-9] +)/(?P [^ /] +)/ $']

这是应该显示主题的topic.html。

{% extends "learning_logs/base.html" %}

{% block content %}

<div class = 'topics-1'>
    <h1> Topics: </h1>

    <ul>
    {% for topic in topics %}
        <li>
         <a href = "{% url 'learning_logs:topic' topic.id topic.type%}">[] {{topic}}</a>
        </li>

        {% empty %}

        <li> No topics have been added yet. </li>

        {% endfor %}
    </ul>
</div>

<a href = "{% url 'learning_logs:new_topic' %}"> Add a new topic :</a>

{%endblock content%}

您可以在主题模型中添加类型,即:

class Topic(models.Model):
topic_choices= (
                ('t_topic', 't_topic',)
                ('f_topic', 'f_topic',)
)
text = models.CharField(max_length = 200)
date_added = models.DateTimeField(auto_now_add = True)
owner = models.ForeignKey(User, on_delete = models.CASCADE) 
type = models.CharField(max_length=100, choices=topic_choices)

def __str__(self):

    return self.text

然后在检索主题时,请指定类型:

def f_topic(request, topic_id, type):
    topic = Topic.objects.get(id=topic_id, type='f_topic')

您必须更改自己的网址,但可以将其添加到链接中:

<a href = "{% url 'learning_logs:topic' topic.id topic.type %}">

为此,请在上下文中添加模板的名称。

context = {'topic': topic, 'entries': entries, 'images': images, 'template':example.html}

然后在模型主题中,添加一个字段:

template = models.CharField(max_length = 200).

在保存新创建的主题的视图中,获取template的请求值:

topic.template = request.POST.get("template")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM