[英]Is there a way to pass URL template tag from database to template in Django?
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.我想使用
{% url 'viewname' otherchar.slug %}
方法链接到其他字符。 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?我知道这可能是不可能的,但是有没有办法让 Django 看到那条线并将其变成绝对的 URL 就像我手动将 ZE6B391A8D2C4D459702A23A8B6585 标签添加到页面中一样?
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. 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模型.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视图.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网址.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:您可以将
get_context_data
中的内容呈现为模板:
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
.other_attribute = Template(
self.object.content
).
render(RequestContext(self.request, context)
)
return context
where .other_attribute
is an identifier you do not use as a field, property, method, etc.其中
.other_attribute
是您不用作字段、属性、方法等的标识符。
and then render this in the "outer" template with:然后在“外部”模板中渲染它:
<div class="col-md-8 p-4 text-justify border border-secondary rounded shadow">
{{ character
.other_attribute|safe }}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.