简体   繁体   中英

How to pass two different arguments in a single function in django?

Python newbie here, I am trying to create a web-app where shippers can post their truck-loads on sale and accept bids from transporters, and the transporters can post their bids on loads.

I have a list view function in my views.py, where a shipper can see all the bids posted by different suppliers, and then he can 'assign' one supplier for that post-load.

负载

For that I created this function which takes 2 arguments : the quiz.id (which is the unique primary key of the post-load) and the supplier's ID.

urls.py

 path('confirm/<int:pk>/', teachers.ConfirmRFQ, name='ConfirmRFQ'),

views.py

@login_required
def ConfirmRFQ (request, pk,bi):
    quiz = Quiz.objects.get(pk=pk)
    bid = Bid.objects.get(pk=bi)
    quiz.status = 'Assigned'
    bid.confirmed = 'Assigned'
    quiz.save()
    bid.save()

    return redirect('teachers:quiz_change_list')

The function call in template:

 <a href="{% url 'teachers:ConfirmRFQ' quiz.pk %}" class="btn btn-primary">Assign</a>

Is there something wrong here ? Can I not pass two arguments as such ? Because I keep getting the

NoReverseMatch at /teachers/quiz/13/results/

Reverse for 'ConfirmRFQ' with arguments '(13, 33)' not found. 1 pattern(s) tried: ['teachers/confirm/(?P[0-9]+)/$']

error.

Not sure if model is necassary here, but here it is:

models.py

class Quiz(models.Model):
    bid_status_choices = (('Active', 'Active'), ('Assigned', 'Assigned'), ('Dispatched', 'Dispatched'), ('Delayed', 'Delayed'),
            ('Delivered', 'Delivered'))
    mtypes =(('Fragile','Fragile'),('Non-Fragile','Non-Fragile'))

    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='quizzes')
    name = models.CharField(max_length=255)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE, related_name='quizzes')
    origin = models.CharField(max_length=255, default=0)
    destination = models.CharField(max_length=255,default=0)
    total_trucks = models.IntegerField(default=0)
    material_type = models.CharField(max_length=255,default=0, choices=mtypes)
    scheduled_date = models.CharField(max_length=255,default=0)
    offered_price = models.IntegerField(default=0)
    status = models.CharField(max_length=255, default='Active', choices=bid_status_choices)

    def __str__ (self):
        return self.name

class Bid(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='answers')
    text = models.CharField('Answer', max_length=255)
    bid_amount = models.CharField('bid_amount', max_length=255,default=0)
    is_correct = models.BooleanField('Correct answer', default=False)
    bid_date = models.DateTimeField(auto_now_add=True)

    def __str__ (self):
        return self.text

You can extend your path with an aribitrary number of parameters, here for example the pattern should probably look like:

path('confirm/<int:pk>/', teachers.ConfirmRFQ, name='ConfirmRFQ'),

So the view requires two parameters here: pk and bi .

In your template for example, you can then retrieve the URL by passing two parameters like:

{% url 'teachers:ConfirmRFQ' pk=quiz.pk  %}

Note however that updating entities is typically not done through a GET request (since a GET request should not have any side-effects), but through a POST request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM