【问题标题】:Put a button in the creation of a model在创建模型中放置一个按钮
【发布时间】:2021-08-14 17:45:06
【问题描述】:

我想确保除了标题、颜色和描述之外,我还想确保当我创建一个模型时,它会让我选择在用户单击按钮时呈现它的 url。有可能吗?

models.py

from django.db import models
from colorfield.fields import ColorField

class Aziende(models.Model):
    immagine = models.ImageField()
    nome = models.CharField(max_length=250)
    prezzo = models.FloatField()
    descrizione = models.CharField(max_length=250)
    nome_color = ColorField(default='#FF0000')

    def __str__(self):
        return self.nome

    class Meta:
        verbose_name_plural = "Aziende"

home.html

[...]
<div class="container">
  <div class="row">
    {% for Aziende in Borsa %}
    <div class="col"><br>
      <div class="container text-center">
      <div class="card" style="width: 18rem;">
      <img src="{{ Aziende.immagine.url }}" class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="title" style="color: {{Aziende.nome_color}}" >{{Aziende.nome}}</h5>
        <p class="card-text">{{Aziende.descrizione}}</p>
        <p class="card-text fst-italic text-primary">Valore Attuale: €{{Aziende.prezzo}}</p>
        <a href="#" class="btn btn-primary">Più info</a>
      </div>
      </div>
      </div>
  </div>
  {% endfor %}
  </div>
</div>
[...]

urls.py (1)

[..]
urlpatterns = [
    path('admin/', admin.site.urls),
    path('Borsa/', include('Borsa.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('', TemplateView.as_view(template_name='home.html'), name='home'),
]+static(settings.MEDIA_URL, document_root =settings.MEDIA_ROOT)

urls.py (2)

[...]
urlpatterns = [
    path('', views.Borsa),
    path('Investimenti', views.Investimenti, name='Investimenti'),

]

提前致谢!

【问题讨论】:

  • 你是说下拉菜单吗?
  • 没有一键...

标签: django sqlite django-models


【解决方案1】:

你可以这样做

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="/">Home</a>
    <a href="/admin/">admin</a>
    <a href="/Borsa/">Borsa</a>
    <a href="/accounts/">Link 3</a>
  </div>
</div>

【讨论】:

  • 感谢您的支持,但我明白这一点。我打算为每个模型创建一个具有不同 url 的按钮。如何从数据库中获取它?
  • 到目前为止您是否创建了任何 URL?您想放在前端进行导航。
  • 是的,我有这些。
  • 好的,我做到了。 ,.,
  • 我编辑了我的答案,这是你想做的吗?
猜你喜欢
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多