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

时间:2018-12-01 00:25:35

标签: python django forms model

所以我有一个主题。

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 /遇到了相同的NoReverseMatch错误 找不到带有参数'(10,'')'的'topic'反向。尝试了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%}

2 个答案:

答案 0 :(得分:0)

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

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 %}">

答案 1 :(得分:0)

要使其成为可能,请在上下文中添加模板的名称。

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

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

template = models.CharField(max_length = 200).

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

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