• 1401/08/04

گرفتن اینپوتی که checked است :

با سلام من می خوام چند اینپوت اضافه کنم با تایپ radio می خوام یک کاری کنم که value اونی که checked است رو بگیرم چجوری اگه میشه بگین 

  • 1401/08/04
  • ساعت 16:07

سلام اگه میخوای با فرمای جنگو اینکارو انجام بدی میتونی از  widget . forms.RadioSelect استفاده کنی به این صورت 
 

CHOICES = [('accessName1', 'ShowValue1'),
               ('accessName2', 'ShowValue2')]
test = forms.ChoiceField(choices=CHOICES,error_messages={'invalid_choice':'نوع انتخاب معتبر نمیباشد'},widget=forms.RadioSelect(attrs={'class': 'TEST'}),
            )

اگه با جاوا میخوای اینکارو کنی

یه سری اینپوت تعریف کن 


 <input type="radio"  name="customradio" value="گزینه اول">
 <input type="radio"  name="customradio" value="گزینه دوم">

تو فایل js هم میتونی با jquery مقدار انتخاب شدرو بدست بیاری 

 

const SelectedRadio= $('input[name="customradio"]:checked').val();

  • 1401/08/04
  • ساعت 16:35

ممنون 👌👌👌🙏🙏🙏🙏

 


  • 1401/08/04
  • ساعت 16:43

خواهش میکنم 😉


  • 1401/08/04
  • ساعت 17:01

چرا من روی هر کدوم که میزنم در اینپکت checked نمی خوره

کد html

 {% for color in colors %}
                                         <label>{{ color }}</label> <input id='color-product' type="radio"  name="customradio" value="{{ color }}">
                                        {% endfor %}

کد js

const productCount = $('#product-count').val();
const SelectedRadio= $('input[name="customradio"]:checked').val();
    $.get('/order/add-to-order?product_id=' + productId + '&count=' + productCount + '&color=' + SelectedRadio)

  • 1401/08/04
  • ساعت 17:06

id هاشون یکیه برای همین تداخل میخوره برای درست کردنش id  رو برابر  color-product{color.id}
قرار بدی مشکل درست میشه
راجب اینکه چرا checked نمیاره راستش نمیدونم برا منم اینجوریه 


  • 1401/08/04
  • ساعت 17:23

نه درست نشد 😔😔😔


  • 1401/08/04
  • ساعت 17:24

اطلاعات به درستی انتقال داده میشه؟ ( رنگی که انتخاب میکنی درست فرستاده میشه؟)


  • 1401/08/04
  • ساعت 17:32

نه چون اصلا checked نمیشه که درست فرستاده بشه

 


  • 1401/08/04
  • ساعت 17:39

عکس ویو add-to-order رو میدی لطفا چون برا منم checked نمیاره ولی کار میکنه فکرم نکنم مهم باشه 


  • 1401/08/04
  • ساعت 18:26
def add_product_to_order(request):
    product_id = int(request.GET.get('product_id'))
    count = int(request.GET.get('count'))
    color_product = request.GET.get('color')
    if count < 1 or color_product is '':
        return JsonResponse({
            'status': 'invalid_count'
        })
    elif count > 4 :
        return JsonResponse({
            'status': 'invalid_count'
        })
    
    if request.user.is_authenticated:
        if request.user.address is None:
            return JsonResponse({
                'status': 'is_none_res'
            })
        if request.user.mobile is None or request.user.address_ip is None or  request.user.address is None :
            return JsonResponse({
                'status': 'is_none_res'
            })
        product = Product.objects.filter(id=product_id, is_active=True, is_delete=False).first()
        if product is not None:
            # current_order = Order.objects.filter(is_paid=False, user_id=request.user.id).first()
            current_order, created = Order.objects.get_or_create(is_paid=False, user_id=request.user.id)
            current_order_detail = current_order.orderdetail_set.filter(product_id=product_id).first()
            if current_order_detail is not None:
                current_order_detail.count += count
                current_order_detail.color = color_product
                current_order_detail.save()
            else:
                new_detail = OrderDetail(order_id=current_order.id, product_id=product_id, count=count, color=color_product)
                new_detail.save()

            return JsonResponse({
                'status': 'success'
            })
        else:
            return JsonResponse({
                'status': 'not_found'
            })
    else:
        return JsonResponse({
            'status': 'not_auth'
        })

  • 1401/08/04
  • ساعت 18:33

بعد خط چهارم که این هست 


color_product = request.GET.get('color')

میشه یه print بگیری از color  ایی که دریافت میشه 
 

print(color_product)

موقع ثبت از run ide هم عکس میدی اگه اروری میده با console log مرورگر 


  • 1401/08/04
  • ساعت 18:58

رو سرور کجا پرینت می کنه

تازه وقتی که میزنم رو افزودن به سبد خرید داخل سبد خرید قسمت رنگ میاره undefined

 


  • 1401/08/04
  • ساعت 19:09

پابلیشه؟ که اینطور 

به نظر میاد کار میکنه فقط تو گرفتن اطلاعات رنگ مشکل هست که اینارو امتحان کن ببین چی میشه

اگه undefined هست فقط یه چیزی مدل color رو چجوری ساختی اینجوری 

 

class ProductColors(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True)
    title = models.CharField(max_length=100)

یا فقط یه فیلد معمولی تو order detail ساختین 
اگه به روش اول ساختین توی تمپلیت یجور دیگه اطلاعات رنگ انتخاب شدرو بفرستین

 

{% for color in colors %}
 <label>{{ color }}</label> 
 <input id='color-product' type="radio" name="customradio" value="{{ color.id }}"> 
 {% endfor %}

و توی view add to order

 

اون قسمتی که محصولو دریافت میکنی با id رنگ هم دریافت کن

product = Product.objects.filter(id=product_id, is_active=True, is_delete=False).first()
the_color = ProductColors.object.filter(id=color_product).first()

if product is not None and the_color is not None:

 


  • 1401/08/04
  • ساعت 19:15

قسمت دوم نفهمیدم دقیقا باید چکار کنم


  • 1401/08/04
  • ساعت 19:24

خب ببین من کاری که میخوایو رو پروژه خودم تست کردم کار کرد قبل هر کاری یه cntrl f5  رو امتحان کن 
(قسمت دوم  value رو به id رنگ تغییر دادم که نیازی نیست انجام بدی اگه مدلی نساختی و یه فیلد سادس تو orderDetail)

اگه نشد از راه مدل امتحانش کن

 


  • 1401/08/04
  • ساعت 19:30

کنترل f5 زدم درست شد خیلی ممنون 🙏🙏🙏🙏🙏🙏🙏🙏


  • 1401/08/04
  • ساعت 19:34

خوشحال شدم فقط یه چیز دیگه ایین قسمت از کدت که 

current_order_detail = current_order.orderdetail_set.filter(product_id=product_id).first() if current_order_detail is not None:

رو به این تغییر بده که بتونی از یه محصول با رنگای مختلف بتونی تو سبد خریدت داشته باشی

current_order_detail = current_order.orderdetail_set.filter(product_id=product_id, color=color_product).first()

 


  • 1401/08/04
  • ساعت 21:00

ممنون 🙏🙏🙏🙏🙏


  • 1401/08/07
  • ساعت 09:15

سلام دوست من

خوشحالم که مشکلتون برطرف شده

ممنونم از اقای اکبری بابت کمکشون

موفق باشین :)


logo-enamadlogo-samandehi