【发布时间】:2021-07-06 15:59:13
【问题描述】:
我的网页上有一个网址是“http://127.0.0.1:8000/affiliation/link/10006/”。
在上面的网址中,我想添加用户 ID,使其看起来像:“http://127.0.0.1:8000/affiliation/link/01/10006/”这样的东西,而“01”是上传产品的用户的用户 ID。
以下是文件。
观看次数:
#Display individual product and render short links for all using pyshorteners
def link_view(request, uid):
results = AffProduct.objects.get(uid=uid)
slink = "http://127.0.0.1:8000/" + request.get_full_path()
shortener = pyshorteners.Shortener()
short_link = shortener.tinyurl.short(slink)
return render(request, 'link.html', {"results": results, "short_link": short_link})
型号:
#Product details uploaded
class AffProduct(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='foo')
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)
网址:
urlpatterns = [
path('link/<int:uid>/', views.link_view, name='link_view')
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
【问题讨论】: