【问题标题】:i m using url slug for my pages, but i have to manually type the slug , it doesnot appear when i click on the button我正在为我的页面使用 url slug,但我必须手动输入 slug,当我单击按钮时它不会出现
【发布时间】:2021-08-18 19:38:30
【问题描述】:

我正在尝试生成不同的页面,但是当我单击时只出现实际页面,而不是加载了值的页面。 但是当我在 url 中手动添加 uid 时,页面就会出现。

这是我的看法,这两个是相关的,

def link_view(request, uid):
    results = AffProduct.objects.get(uid=uid)
    return render(request, 'link.html', {"results":results})

def link(request):
    return render(request, 'link.html')

这是我的网址格式,

 path('link/', views.link, name='link'),
 path('link/<int:uid>', views.link_view, name='link_view'),

这是模型,

class AffProduct(models.Model):
    user=models.ForeignKey(User, on_delete=models.CASCADE)
    product_title = models.CharField(max_length=255)
    uid = models.IntegerField(primary_key=True)
    specification = models.CharField(max_length=255)
    sale_price = models.IntegerField()
    discount = models.IntegerField()
    img1 = models.ImageField(max_length=255, null=True, blank=True, upload_to="images/")
    img2 = models.ImageField(max_length=255, null=True, blank=True, upload_to="images/")
    promote_method = models.TextChoices
    terms_conditions = models.CharField(max_length=255, null=True)
    promote_method = models.CharField(
        max_length=20,
        choices=promote_choices,
        default='PPC'
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

这是我的 Html 部分,

#What i want to achieve is when i click on 'Get Link', it should redirect to 
path('link/<int:uid>', views.link_view, name='link_view'),

但它在没有 slug 数据的情况下重定向。

<span>
   <a class="Update" href="{% url 'link' %}">Get Link</a>
</span>

【问题讨论】:

  • 你可以发布你的模型
  • 完成,@SreevardhanReddy

标签: python django


【解决方案1】:

试试这个

def link(request):
    results = AffProduct.objects.all()
    return render(request, 'link.html', {"results":results})

#link.html
 {% for product in results %}
   <span>
      <a class="Update" href="{% url 'link_view' product.uid %}">Get Link</a>
   </span>
 {% endfor %}

【讨论】:

  • 这是我在尝试此方法时遇到的错误``` NoReverseMatch at /affiliation/blink_viewproduct/ Reverse for 'link' with arguments '(10001,)' not found.尝试了 1 种模式:['affiliation/link/$'] ```
  • 只需更新&lt;a class="Update" href="{% url 'link_view' product.uid %}"&gt;Get Link&lt;/a&gt; 应该是link_view
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
相关资源
最近更新 更多