模板中的 href 和 Url 在 Django 中不起作用
The href in template and the Url are not working in Django
我是 django 的新手。我已将 table 单元格设为可点击,但当我点击该单元格时,没有任何反应。
template
:
{% for each in object_list %}
<tr>
<td id="{{ each.id }}" data-href="http://127.0.0.1:8000/{{ each.region }}/{{ each.id }}"></td>
</tr>
{% endfor %}
需要 view
的一部分:
class MeterDetailView(DetailView):
model = Meter
template_name = 'meter/specificMeter.html'
def get_context_data(self, **kwargs):
pk = kwargs.get('pk',None)
obj = Meter.objects.get(pk=pk)
url
:
path('<int:region>/<int:pk>/', views.MeterDetailView.as_view(), name='detail')
model
:
class Meter(models.Model):
region = models.PositiveSmallIntegerField()
UserId = models.BigIntegerField(default=0,null=False)
怎么了?谢谢大家。
要使单元格在单击后重定向,您可以使用 onclick
属性。在您的情况下,我们将不得不稍微更改代码:
{% for each in object_list %}
<tr>
<td id="{{ each.id }}" onclick="window.location='http://127.0.0.1:8000/{{ each.region }}/{{ each.id}}'"></td>
</tr>
{% endfor %}
可以在this问题
中找到类似的方法
就像您的问题下的评论中已经解释的那样,data-href
只是 dataset
HTML 属性 的变体。您可以在这里阅读更多信息:dataset
最简单的方法:将所有单元格数据插入 <a href="link"></a>
标签内。 This question's 所选答案可以告诉你更多信息。
也可以通过向您的代码添加 onclick
事件来处理 data-href
:
<td id="{{ each.id }}" data-href="{% url 'detail' each.region each.id %}" onclick="makeCellClickable()" class="some-class></td>
请注意,我放置了 url
标签而不是直接 link 作为 data-href。
除此之外,您需要在 linked js 文件或 html 文件中的 <script></script>
标签中指定 makeCellClickable()
函数。不要忘记在您的样式表中添加 class(例如 some-class
),这将更改背景、光标或您希望向用户显示该单元格可点击的方式。
function makeCellClickable() {
window.location = this.event.target.dataset.href
}
如果你想指定url你应该使用如下:
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',YourView, name='UseForSpecifyUrl'),]
.html
<div class="col-6 text-right">
<a href="{% url 'UseForSpecifyUrl' %}" class="text-primary"><small>Do something</small></a>
</div>
我是 django 的新手。我已将 table 单元格设为可点击,但当我点击该单元格时,没有任何反应。
template
:
{% for each in object_list %}
<tr>
<td id="{{ each.id }}" data-href="http://127.0.0.1:8000/{{ each.region }}/{{ each.id }}"></td>
</tr>
{% endfor %}
需要 view
的一部分:
class MeterDetailView(DetailView):
model = Meter
template_name = 'meter/specificMeter.html'
def get_context_data(self, **kwargs):
pk = kwargs.get('pk',None)
obj = Meter.objects.get(pk=pk)
url
:
path('<int:region>/<int:pk>/', views.MeterDetailView.as_view(), name='detail')
model
:
class Meter(models.Model):
region = models.PositiveSmallIntegerField()
UserId = models.BigIntegerField(default=0,null=False)
怎么了?谢谢大家。
要使单元格在单击后重定向,您可以使用 onclick
属性。在您的情况下,我们将不得不稍微更改代码:
{% for each in object_list %}
<tr>
<td id="{{ each.id }}" onclick="window.location='http://127.0.0.1:8000/{{ each.region }}/{{ each.id}}'"></td>
</tr>
{% endfor %}
可以在this问题
中找到类似的方法就像您的问题下的评论中已经解释的那样,data-href
只是 dataset
HTML 属性 的变体。您可以在这里阅读更多信息:dataset
最简单的方法:将所有单元格数据插入 <a href="link"></a>
标签内。 This question's 所选答案可以告诉你更多信息。
也可以通过向您的代码添加 onclick
事件来处理 data-href
:
<td id="{{ each.id }}" data-href="{% url 'detail' each.region each.id %}" onclick="makeCellClickable()" class="some-class></td>
请注意,我放置了 url
标签而不是直接 link 作为 data-href。
除此之外,您需要在 linked js 文件或 html 文件中的 <script></script>
标签中指定 makeCellClickable()
函数。不要忘记在您的样式表中添加 class(例如 some-class
),这将更改背景、光标或您希望向用户显示该单元格可点击的方式。
function makeCellClickable() {
window.location = this.event.target.dataset.href
}
如果你想指定url你应该使用如下:
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',YourView, name='UseForSpecifyUrl'),]
.html
<div class="col-6 text-right">
<a href="{% url 'UseForSpecifyUrl' %}" class="text-primary"><small>Do something</small></a>
</div>