weixin_33739541 2014-08-04 13:38 采纳率: 0%
浏览 20

Django表单无法验证

I have been spending multiple days trying to get to the bottom of this problem but I still haven't figured it out.

I'm working on a website that allows people to share or rent musical instruments to peers near them. One of the pages is "Offer Instrument", and on this page there is a form. The form has two main purposes; to allow you to search for someone near you looking for this instrument, or to post your listing to the site.

If you click "Search", the site only looks at two pieces of data, the type of instrument and the location. If you click "Post", the site looks at all the input boxes (i.e. size, age, brand, etc), and enters it in.

When you click "Search", an Ajax request is sent to the view instrument_offering_search(request) where it is supposed to processed and results are supposed to be returned. However the form object is never returning valid.

def instrument_offer_search(request):    
    if request.method == 'POST':
        form = InstrumentOfferSearchForm(request.POST)
        if form.is_valid():
             #always returning false
        else: 
             .....

When you click "Post", an Ajax request is sent to the view instrument_offering(request) where it is processed and added to the database. This works fine

def instrument_offering(request): 
    if request.method == 'POST':
        form = InstrumentOfferForm(request.POST)
        if form.is_valid():
             #works!

I've verified by using both Chrome tools and pdb that the Search button is definitely sending the POST data.

Why isn't the form being valid?

  • 写回答

0条回答 默认 最新

    报告相同问题?