总之,我正在网上商店和django管理员,我显示经过身份验证的用户'用户名'但有些用户没有经过身份验证
因此,如果经过身份验证的用户会订购某些东西,那么我可以获得他们的数据。 但是,如果未经身份验证的用户会订购?什么在django管理员中显示? 如果我将从用户那里仅请求他的名字,那就没有结果。因为我们知道django需要完全授权
models.py
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, through=CartProduct)
class Meta:
verbose_name = 'Cart'
verbose_name_plural = 'Cart'
def __str__(self):
return self.user.username # That for authenticated users
views.py
class CartView(View):
def get(self,request,*args,**kwargs):
cart_id = request.session.get('cart_id')
if cart_id == None:
cart = Cart()
cart.save()
request.session['cart_id'] = cart_id
cart = Cart.objects.get(id=cart_id)
product_id = request.GET.get('product')
delete_product = request.GET.get('delete')
if product_id:
product_instance = get_object_or_404(Product, id=product_id)
amount = request.GET.get('amount')
cart_product = CartProduct.objects.get_or_create(cart=cart, product=product_instance)[0]
if delete_product:
cart_product.delete()
else:
cart_product.amount = amount
cart_product.save()
return HttpResponseRedirect('/')
答案 0 :(得分:1)
您可以使用IP地址,但这可能会造成混淆/多人可以共享。
所以我的建议是使用Django的内置session framework。
从this stackoverflow answer拉出,您会看到您可以从<input id="txtinpt" type="text" />
<svg viewBox="-50 50 200 200" width="300" height="180" style="background-color: orange">
<path fill="none" stroke="#000" stroke-width="2" id="sig1"
d="m64.5,222.28333c-1,1 -2,1 -4,1c-4,0 -7.513,0.40602 -9,-2c-3.32501,
-5.37999 -5.99594,-22.86484 -7,-34c-1.34708,-14.93939 0.38828,
-24.33003 6,-34c3.61945,-6.23694 9.64749,-10.97249 14,-12c1.9465,
-0.45949 2.29291,-0.70711 3,0c3.53552,3.53554 1,12 1,19c0,5 0,10 0,
14c0,1 0.57956,0.91418 0,-1c-1.04483,-3.45085 -5.47739,-6.63243 -10,
-13c-3.3765,-4.75388 -7.39902,-10.8981 -9,-18c-1.33768,-5.93384 -2.68976,
-10.03406 -2,-16.99999c0.50244,-5.0742 2.31732,-8.31069 11,-12c7.58951,
-3.22482 13.93796,-6.49829 20,-7c1.99319,-0.16496 2,1 2,3c0,2 -0.17194,
5.56063 -1,5c-4.45929,-3.01908 -6.47394,-14.22683 -11,-24c-8.08334,
-17.45451 -14,-25 -18,-29c-1,-1 -1,0 -1,9c0,11 3.33633,32.93664 6,
48c2.29031,12.95199 3.26926,28.50887 7,37.99999c5.58417,14.20624 11.49622,
18.90779 12,23c0.12219,0.99251 -0.0535,1.54051 -2,2c-2.17624,0.51375 -5.2771,
1.77199 -9,0c-4.51468,-2.14885 -7.51373,-2.82375 -7,-5c0.68927,-2.91974 7.8858,
2.32422 16,5c6.91388,2.27995 13.22836,4.85194 16,6c1.30655,0.5412 1,2 0,2c-2,
0 -4.0535,1.4595 -6,1c-2.17624,-0.51375 -5.93292,-5.10446 -9,-7c-6.85815,
-4.23859 -10.08743,-6.79393 -15,-9c-4.07965,-1.83203 -6.29291,-3.70711 -7,
-3c-1.41422,1.41423 2.75223,4.28316 5,6c6.40714,4.89374 11.01703,6.4124 16,
6c6.06204,-0.50171 13.57114,-4.38507 19,-10c7.92529,-8.19693 7,-12 7,-14c0,
-2 -2,-3 -2,-3c-2,0 -2.1731,-0.14726 -3,1c-2.6149,3.62799 -5.96207,13.5372 -11,
22c-8.38965,14.09303 -17.24722,22.79562 -32,36c-9.5423,8.54077 -23.15686,
13.57971 -29,16c-2.06586,0.85571 -3,0 -3,-3c0,-7 2.49875,-17.94672 3,-25c0.638,
-8.97736 -0.01624,-16.20224 6,-24c3.91138,-5.06963 13.47281,-9.96919 25,-12c6.89383,
-1.21451 19.03406,-2.68976 26,-2c10.14841,1.0049 14.64886,2.19577 17,6c1.05145,
1.70131 -0.29291,2.29289 -1,3c-0.70709,0.70711 -1,1 -2,1l0,0l-2,0" />
</svg>
对象中收集当前会话密钥。
然后只需更改您的购物车型号:
request
当然,您需要确保在创建时将request.session添加到购物车对象。
最后,似乎会话对象有时候仍然是空的,所以如果你真的不想要一个&#34; Anonymous&#34;那么你应该强制保存它。你的管理员行。 This answer使会话强制保存非常简单:
from django.contrib.sessions.models import Session
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
session = models.ForeignKey(Session, null=True, blank=True)
products = models.ManyToManyField(Product, through=CartProduct)
class Meta:
verbose_name = 'Cart'
verbose_name_plural = 'Cart'
def __str__(self):
if self.user :
return self.user.username # For for authenticated users
else if self.session :
return self.session.session_key
else :
return "Anonymous"
答案 1 :(得分:0)
在类中编写一个调度方法,以便您可以检查用户是否经过身份验证
request.user.is_authenticated
将返回用户身份验证的状态。如果用户未经过身份验证,则根据需要保存用户