如何在django框架中连接字符串
How to concatenate string in django framework
我想连接一个字符串以在我的 Django 数据表列之一中获取产品 link。
我想要这样的结果:
https://www.aliexpress.com/item/1005003324927716.html
产品 ID 存储在我的 mongodb 数据库中。
这是我试图在 datatable.html 中写的内容,但我得到一个空列:
{% for product in products %}
<tr>
<td>
{% with "https://www.aliexpress.com/item/"|add:{{product.productId}}|add:".html" as template %}
{% endwith %}
</td>
</tr>
{% endfor %}
然后将其作为 href link 作为此语法:
<td><a href="{{product.Productlinks }}"> Click here</a></td>
views.py :
def datatable_view(request):
client = MongoClient("mongodb://localhost:27017/")
db = client["aliexpress"]
col = db["listproducts"]
products = col.find()
context = {'products' : products}
return render(request,'datatable.html', context)
models.py :
class Datatable(models.Model):
Title = models.CharField('Title',max_length=500),
Price = models.DecimalField('Price',decimal_places = 3, max_digits = 10000),
Currency = models.CharField('Currency',max_length=500),
Stars = models.DecimalField('Stars',decimal_places = 3 , max_digits = 10000),
Orders = models.PositiveIntegerField('Orders',max_length=500),
Shipcost = models.CharField('Shipcost',max_length=500),
Supplier = models.CharField('Supplier',max_length=500),
Productlinks = models.CharField('Productlinks',max_length=700)
我在使用 django 方面还很陌生,如果你能在这个问题上帮助我,我将不胜感激。
谢谢!
简单点,在您的 Product
模型中添加一个 属性
class Product(models.Model):
...
@property
def link(self):
return 'https://www.aliexpress.com/item/' + str(self.productId) + '.html'
<td><a href="{{product.link}}"> Click here</a></td>
更新
您可以通过这样做使其更加简单:
<td><a href="https://www.aliexpress.com/item/{{product.productId}}.html">Click here</a></td>
我想连接一个字符串以在我的 Django 数据表列之一中获取产品 link。
我想要这样的结果: https://www.aliexpress.com/item/1005003324927716.html
产品 ID 存储在我的 mongodb 数据库中。 这是我试图在 datatable.html 中写的内容,但我得到一个空列:
{% for product in products %}
<tr>
<td>
{% with "https://www.aliexpress.com/item/"|add:{{product.productId}}|add:".html" as template %}
{% endwith %}
</td>
</tr>
{% endfor %}
然后将其作为 href link 作为此语法:
<td><a href="{{product.Productlinks }}"> Click here</a></td>
views.py :
def datatable_view(request):
client = MongoClient("mongodb://localhost:27017/")
db = client["aliexpress"]
col = db["listproducts"]
products = col.find()
context = {'products' : products}
return render(request,'datatable.html', context)
models.py :
class Datatable(models.Model):
Title = models.CharField('Title',max_length=500),
Price = models.DecimalField('Price',decimal_places = 3, max_digits = 10000),
Currency = models.CharField('Currency',max_length=500),
Stars = models.DecimalField('Stars',decimal_places = 3 , max_digits = 10000),
Orders = models.PositiveIntegerField('Orders',max_length=500),
Shipcost = models.CharField('Shipcost',max_length=500),
Supplier = models.CharField('Supplier',max_length=500),
Productlinks = models.CharField('Productlinks',max_length=700)
我在使用 django 方面还很陌生,如果你能在这个问题上帮助我,我将不胜感激。 谢谢!
简单点,在您的 Product
模型中添加一个 属性
class Product(models.Model):
...
@property
def link(self):
return 'https://www.aliexpress.com/item/' + str(self.productId) + '.html'
<td><a href="{{product.link}}"> Click here</a></td>
更新
您可以通过这样做使其更加简单:
<td><a href="https://www.aliexpress.com/item/{{product.productId}}.html">Click here</a></td>