I apologize if my question is poorly worded. Basically, I am making a database of fictional characters and in the database, I have a biography of the character. I would like to link to other characters using the {% url 'viewname' otherchar.slug %}
method. However, all I get back from the database is literally that line of code.
I understand it may not be possible, but is there a way to get Django to see that line and turn it into an absolute URL like it would if I manually added the URL tag into the page?
Something to add - I am using TinyMCE so the content in the database is being saved with HTML - I want to be able to save external links, subheadings, and whatnot, which is why I chose to use TinyMCE and its HTML field.
models.py
class Character(models.Model):
name = models.CharField(max_length = 255)
faction = models.IntegerField(default = 0)
rankIMG = models.ImageField(upload_to = 'rankIMG/', blank = True)
department = models.IntegerField(default = 0)
content = HTMLField()
slug = models.SlugField()
views.py
class CharacterFull(DetailView):
model = Character
context_object_name = 'character'
def get_context_data(self, **kwargs):
context = super(CharacterFull, self).get_context_data(**kwargs)
context['factionDict'] = charFaction
context['deptDict'] = charDepartment
return context
urls.py
app_name = 'LCARS'
urlpatterns = [
path('', LCARSView.LCARSHome.as_view(), name = 'lcarsHome'),
path('Characters/', LCARSView.Characters.as_view(), name = 'characterHome'),
path('Characters/p/<slug:slug>', LCARSView.CharacterPartialView, name = 'charPartialView'),
path('Characters/<slug:slug>/', LCARSView.CharacterFull.as_view(), name = 'characterView'),
]
template (relevant code)
<div class="col-md-8 p-4 text-justify border border-secondary rounded shadow">
{{ character.content|safe }}
</div>
page source example (if it's helpful)
<p dir="ltr">He was also assigned <a href="{% url 'LCARS:characterView' character.slug %}">Commander Shampoo</a> as his...
I appreciate any assistance you guys can be of. I can't seem to find anything on here or on Google. Thanks!
You can render the content in the get_context_data
as a template:
from django.template import Template, RequestContext
class CharacterFull(DetailView):
model = Character
context_object_name = 'character'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['factionDict'] = charFaction
context['deptDict'] = charDepartment
self.object = Template(
self.object.content
).RequestContext(self.request, context)
return context
where .other_attribute
is an identifier you do not use as a field, property, method, etc.
and then render this in the "outer" template with:
<div class="col-md-8 p-4 text-justify border border-secondary rounded shadow">
{{ character|safe }}
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.