Reputation: 63
So I have a Topic.
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
The topic is retrieved and displayed based on the date it was created.
def topics(request):
"""Show all topics """
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
What I would like to do is to have the Topic somehow distinguished based on which web page it was created from.
And then I want the same topic to be displayed on the same webpage.
As you can see here, below. The same topic is displayed on topic.html and f_topic.html because the same topic_id is used.
I want to have it so that if the topic was created on topic.html then it is displayed on topic.html. If it is created on f_topic.html then it is displayed on 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)
How the topic is saved as 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>
I get the same error of NoReverseMatch at /topics/ Reverse for 'topic' with arguments '(10, '')' not found. 1 pattern(s) tried: ['topics/(?P[0-9]+)/(?P[^/]+)/$']
Here is the topics.html that should display the topics.
{% 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 %}
Upvotes: 0
Views: 50
Reputation:
You could add a type to your Topic model i.e.:
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
and then when you retrieve the topics specify the type:
def f_topic(request, topic_id, type):
topic = Topic.objects.get(id=topic_id, type='f_topic')
You would have to change your urls, but you can add it to the link:
<a href = "{% url 'learning_logs:topic' topic.id topic.type %}">
Upvotes: 0
Reputation: 1046
To make this possible, add to context the name of the template.
context = {'topic': topic, 'entries': entries, 'images': images, 'template':example.html}
Then in the model topic, add a field :
template = models.CharField(max_length = 200).
In the view where you save newly created topics, get the request value of template :
topic.template = request.POST.get("template")
Upvotes: 0