Friday, November 20, 2015

Re: matching query does not exist.

Oh, this worked - thank you so much for the code help and explanation!

Much appreciated. 

B  

On Thursday, November 19, 2015 at 7:59:26 PM UTC-8, Andrew Farrell wrote:
Hi Becka,

So it seems like, from reading your code, that I can diagnose your issue.
When a user clicks "sign up", in say the Thursday Shifts Available table, the browser sends a POST from this form:
<form method="post">     
{% csrf_token %}
            <div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
  </div>
<input type="submit" value="sign up">
</form>
This POST includes the keys 'csrf_token', 'camper, 'meal', & 'meal' but not 'shift_id'. Therefore, in 

    if request.method == 'POST':
        shift_id = request.POST.get('shift_id')
        signup_for_shift(shift_id, request.user)
        return redirect('signup')
request.POST.get('shift_id') returns None.
When that gets passed to mealShifts.objects.get(pk=shift_id), it  raises the error.
You can confirm that by inserting the line 
  import pdb;pdb.set_trace()
before that line and run `print(request.POST.get('shift_id'))` when the django toy server hits that breakpoint.

-- Andrew Farrell

On Thu, Nov 19, 2015 at 9:31 PM, Becka R. <beck...@gmail.com> wrote:
Hi --
 
I'm working on a personal project, and have been stuck on this problem for a bit of time.  There don't seem to be examples out there for editing tables with data. Is this right? Am I looking in the right places?  I'm still pretty new at Django, and learning largely on my own. 

I have  a template wherein people can sign up for meal shifts at Burning Man. It doesn't use a form, because the table is populated with data, but until someone signs up for a shift, "camper" is set to None.  The page is rendering and displaying data correctly, but when I go to add myself as a logged in user, I get the following error:

DoesNotExist at /signup/

mealShifts matching query does not exist.

Code is below. Thanks for any help.  

Signup is the name of the template and the view function.  mealShifts (which I know needs to be capitalized) is the name of the model. 

Here's the view:

def signup_for_shift(shift_id, camper):
    shift = mealShifts.objects.get(pk=shift_id)
    if shift.camper is not None:
        raise ValueError

    shift.camper = camper
    shift.save()



@login_required(login_url='login.html')
def signup(request):
    model = mealShifts
    user = request.user
    poss_shifts = mealShifts.objects.all().order_by('day')
    day_choices = range(0, 6)
    meal_choices = ['Breakfast', 'Dinner']
    shift_choices = ['Chef', 'Sous_Chef', 'KP']
    username = None
    

    sundayShiftsAvail = mealShifts.objects.filter(day=0, assigned=False)
    sundayShiftsTaken = mealShifts.objects.filter(day=0, assigned=True)

    mondayShiftsAvail = mealShifts.objects.filter(day=1, assigned=False)
    mondayShiftsTaken = mealShifts.objects.filter(day=1, assigned=True)

    tuesdayShiftsAvail = mealShifts.objects.filter(day=2, assigned=True)
    tuesdayShiftsTaken = mealShifts.objects.filter(day=2, assigned=True)

    wednesdayShiftsAvail = mealShifts.objects.filter(day=3, assigned=False)
    wednesdayShiftsTaken = mealShifts.objects.filter(day=3, assigned=True)

    thursdayShiftsAvail = mealShifts.objects.filter(day=4, assigned=False)
    thursdayShiftsTaken = mealShifts.objects.filter(day=4, assigned=True)

    fridayShiftsAvail = mealShifts.objects.filter(day=5, assigned=False)
    fridayShiftsTaken = mealShifts.objects.filter(day=5, assigned=True)

    saturdayShiftsAvail = mealShifts.objects.filter(day=6, assigned=False)
    saturdayShiftsTaken = mealShifts.objects.filter(day=6, assigned=True)

    saturdayShiftsAvail = mealShifts.objects.filter(day=7, assigned=False)
    saturdayShiftsTaken = mealShifts.objects.filter(day=7, assigned=True)
    
    if request.method == 'POST':
        shift_id = request.POST.get('shift_id')
        signup_for_shift(shift_id, request.user)
        return redirect('signup')

    context = RequestContext(request)
    return render_to_response('signup.html', 
        RequestContext(request, {
                'username':username, 'poss_shifts':poss_shifts,
                'sundayShiftsAvail':sundayShiftsAvail, 'sundayShiftsTaken':sundayShiftsTaken,  
                'mondayShiftsTaken':mondayShiftsTaken, 'mondayShiftsAvail':mondayShiftsAvail,  
                'tuesdayShiftsTaken':tuesdayShiftsTaken, 'tuesdayShiftsAvail':tuesdayShiftsAvail,  
                'wednesdayShiftsTaken':wednesdayShiftsTaken, 'wednesdayShiftsAvail':wednesdayShiftsAvail, 
                'thursdayShiftsTaken':thursdayShiftsTaken, 'thursdayShiftsAvail':thursdayShiftsAvail,  
                'fridayShiftsTaken':fridayShiftsTaken, 'fridayShiftsAvail':fridayShiftsAvail,  
                'saturdayShiftsTaken':saturdayShiftsTaken, 'saturdayShiftsAvail':saturdayShiftsAvail
                },))



Here is my model:


class mealShifts(models.Model):
Sunday = "Sunday"
Monday = "Monday"
Tuesday = "Tuesday"
Wednesday = "Wednesday"
Thursday = "Thursday"
Friday = "Friday"
Days = (
(0, "Sunday"),
(1, "Monday"),
(2, "Tuesday"),
(3, "Wednesday"),
(4, "Thursday"),
(5, "Friday"),
(6, "Saturday"),
)
Breakfast = "Breakfast"
Dinner = "Dinner"
Meals = (
(Breakfast, "Breakfast"),
(Dinner, "Dinner"),
)
Chef = "Chef"
Sous_Chef = "Sous-Chef"
KP ="KP"
Shifts = (
(Chef, "Chef"),
(Sous_Chef, "Sous_Chef"),
(KP, "KP"),
)
assigned = models.BooleanField(default=False)
day = models.IntegerField(choices=Days)
meal = models.CharField(max_length = 10, choices=Meals)
shift = models.CharField(max_length = 10, choices=Shifts, default=KP)
camper = models.ForeignKey(User, null=True, blank=True, default=None)

class Meta:
unique_together = ("day", "meal", "shift")

def __str__(self):
return '%s %s %s %s %s %s'%(self.id, self.assigned, self.day, self.meal, self.shift, self.camper)


And my template:

<div class="tablewrap">
<table class="gradienttable">
<thead>
<tr>
<th><p>Day</p></th>
<th><p>Meal</p></th>
<th><p>Shift</p></th>
<th><p>Camper</p></th>
<th><p>Camper</p></th>
</tr>
</thead>

<div class="datatables" id="shiftsTaken">
{% for shift in sundayShiftsTaken %}
<tr class="datarow hideTable">
<td>Sunday</td>
<td>{{shift.meal}}</td>
<td>{{shift.shift}}</td>
<td>{{shift.camper}}</td>
</tr>
{% endfor %}
{% for shifts in mondayShiftsTaken %}
<tr class="datarow hideTable">
<td>Monday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
</tr>
{% endfor %}
{% for shifts in tuesdayShiftsTaken %}
<tr class="datarow hideTable">
<td>Tuesday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
</tr>
{% endfor %}
{% for shifts in wednesdayShiftsTaken %}
<tr class="datarow hideTable">
<td>Wednesday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
</tr>
{% endfor %}
{% for shifts in thursdayShiftsTaken %}
<tr class="datarow hideTable">
<td>Thursday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
</tr>
{% endfor %}
</div>

<div class="showTable datatables" id="shiftsAvailable">
{% for shift in sundayShiftsAvail %}
<tr class="datarow showTable">
<td>Sunday</td>
<td>{{shift.meal}}</td>
<td>{{shift.shift}}</td>
<td>{{shift.camper}}</td>
<td>
<form method="post">    
{% csrf_token %}
<div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
</div>
<input type="submit" value="sign up">
</form>
</td>
</tr>
{% endfor %}
{% for shifts in mondayShiftsAvail %}
<tr class="datarow showTable">
<td>Monday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
<td>
<form method="post">    
{% csrf_token %}
<div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
</div>
<input type="submit" value="sign up">
</form>
</td>
</tr>
{% endfor %}
{% for shifts in tuesdayShiftsAvail %}
<tr class="datarow showTable">
<td>Tuesday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
<td>
<form method="post">    
{% csrf_token %}
<div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
</div>
<input type="submit" value="sign up">
</form>
</td>
</tr>
{% endfor %}
{% for shifts in wednesdayShiftsAvail %}
<tr class="datarow showTable">
<td>Wednesday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
<td>
<form method="post">    
{% csrf_token %}
<div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
</div>
<input type="submit" value="sign up">
</form>
</td>
</tr>
{% endfor %}
{% for shifts in thursdayShiftsAvail %}
<tr class="datarow showTable">
<td>Wednesday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
<td>
<form method="post">    
{% csrf_token %}
<div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
</div>
<input type="submit" value="sign up">
</form>
</td>
</tr>
{% endfor %}
{% for shifts in fridayShiftsAvail %}
<tr class="datarow showTable">
<td>Wednesday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
<td>
<form method="post">    
{% csrf_token %}
<div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
</div>
<input type="submit" value="sign up">
</form>
</td>
</tr>
{% endfor %}
{% for shifts in saturdayShiftsAvail %}
<tr class="datarow showTable">
<td>Wednesday</td>
<td>{{shifts.meal}}</td>
<td>{{shifts.shift}}</td>
<td>{{shifts.camper}}</td>
<td>
<form method="post">    
{% csrf_token %}
<div class="hiddentext">
{{shift.meal}}
{{shift.meal}}
{{shift.camper}}
</div>
<input type="submit" value="sign up">
</form>
</td>
</tr>
{% endfor %}
</div>


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/af844fb2-a1e9-4fe9-97a5-fd61e1b7ae7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/18b242f3-9cb0-4a25-b5c0-d653f38027ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment