Hello fellas!
I’m building a page where I have rendered three objects from a model into a template. I want to be able to display the content of the title on the same page when I click the links. But I have absolutely no idea how to even start.
Can someone please guide me towards the right approach?
In uploaded image, I have rendered the title, date created and the user whom created the content. The titles are linked to another model field called response, which is a API response that I have saved into the database. I would like to display the API response when the titles are clicked on the right side, which represent an A4-sheet.
My model:
class AdminPageModels(models.Model):
username = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
title = models.CharField(_("Title"), max_length=50, null=False, blank=False, default="")
age_group = models.IntegerField(_("Age"), null=True, blank=False)
nr_image = models.PositiveBigIntegerField(_("Images"), null=True, blank=True, validators=[MinValueValidator(1), MaxValueValidator(20)], default=1)
# API settings
temperature = models.DecimalField(_("Temperature"), null=True, blank=False, max_digits=2, decimal_places=1, validators=[MinValueValidator(0), MaxValueValidator(1)])
max_length = models.PositiveIntegerField(_("Max length"), null=True, blank=False, validators=[MinValueValidator(1), MaxValueValidator(4000)])
top_p = models.DecimalField(_("Top P"), null=True, blank=True, max_digits=2, decimal_places=1, validators=[MinValueValidator(0), MaxValueValidator(1)])
frequency_penalty = models.DecimalField(_("Frequency penalty"), null=True, blank=True, max_digits=2, decimal_places=1, validators=[MinValueValidator(-2), MaxValueValidator(2)])
best_of = models.PositiveIntegerField(_("Best of"), null=True, blank=True, validators=[MinValueValidator(1), MaxValueValidator(20)])
presence_penalty = models.DecimalField(_("Presence penalty"), null=True, blank=True, max_digits=2, decimal_places=1 ,validators=[MinValueValidator(-2), MaxValueValidator(2)])
image_description = models.CharField(_("Image description"), null=True, blank=True, max_length=500, default="")
promt = models.TextField(_("Promt"), null=True, blank=False)
date_created = models.DateTimeField(auto_now_add=True, null=True)
response = models.TextField(_("Response"), null=True, blank=False)
def __str__(self):
return self.title
Blockquote
The template:
Blockquote
{% block content %}
{% if request.user.is_authenticated %}
<section class="page__background">
<section class="collections__page__LHS">
<div class="collections">
<table class="table">
<tr class="table_headerRow">
<th>Title</th>
<th>Date</th>
<th>Creator</th>
</tr>
{% for header in AdminPageModels_instance %}
<tr class="rows">
<td>{{header.title}}</td>
<td>{{header.date_created}}</td>
<td>{{header.username}}</td>
</tr>
{% endfor %}
</table>
</div>
</section>
<section class="collections__page__RHS">
<section class="A4_sheet">
</section>
</section>
</section>
{% else %}
<section class="admin__main__page__background">
<div class="restricted__page">
<p class="page__text">You don't have acces to this page</p>
</div>
</section>
{% endif %}
{% endblock content %}
Blockquote
The view function:
Blockquote
def admin_homepage(request):
# handle the case when the response is None or not generated
chatbot = None
user_input_promt = None
user_input_temperature = None
user_input_maxtokens = None
user_input_topP = None
user_input_frequency_penalty = None
if request.method == "POST":
form = AdminPageModelsForm(request.POST)
if form.is_valid():
form.save()
user_input_promt = form.cleaned_data["promt"]
user_input_temperature = float(form.cleaned_data["temperature"])
user_input_maxtokens = int(form.cleaned_data["max_length"])
user_input_topP = float(form.cleaned_data["top_p"])
user_input_frequency_penalty = float(form.cleaned_data["frequency_penalty"])
user_input_presence_penalty = float(form.cleaned_data["presence_penalty"])
user_input_best_of = int(form.cleaned_data["best_of"])
chatbot = chatbot_response(user_input_promt,
user_input_temperature,
user_input_maxtokens,
user_input_topP,
user_input_frequency_penalty,
user_input_presence_penalty,
user_input_best_of)
print(chatbot)
else:
form = AdminPageModelsForm()
context = {"form": form, "chatbot": chatbot}
return render(request, "admin_ui_app/admin.html", context)
def collections(request):
AdminPageModels_instance = AdminPageModels.objects.all()
context = {"AdminPageModels_instance": AdminPageModels_instance}
return render(request, "admin_ui_app/story_collection.html", context)