Django create instance from not working

I have a Django app that is basically a list app for tracking jobs that the user is applying for. Users have applications and applications have questions that the user wants to ask to the interviewers should they get an interview. So I have created a many to many relationship between the models 'Application' and 'Question' called 'Ask'.

class Application(models.Model):
    status_choices = (
        ('NS','Not Started'),
        ('IP','In Progress'),
        ('OH','On Hold'),
    app_status = models.CharField(max_length=2,choices=status_choices)
    position = models.CharField(max_length=255)
    company = models.ForeignKey(Company)
    applied_date = models.DateField(null=True)
    isDeleted = models.BooleanField(default=False, blank=True)
    user = models.ForeignKey(User)
    def __str__(self):
        return self.position

class Questions(models.Model):
    question = models.CharField(max_length=255)
    category = models.CharField(max_length=25)
    isDeleted = models.BooleanField(default=False, blank=True)

class Ask(models.Model): #joining table
    question = models.ForeignKey(Questions)
    app = models.ForeignKey(Application)
    isDeleted = models.BooleanField(default=False, blank=True)

So the idea is that the user creates a list of questions and then when they get an interview, they prepare for it by looking at their list of questions and assigning them to the application through a template. Questions.html:

{% extends 'base.html' %}

{% block body %}
<h1> Questions </h1>
    <div class="table-responsive" align="center">
        <table class="table-hover" width="75%">
                <th> Category </th>
                <th> Question </th>
                <th> <a href="{% url 'JQ:create_question' %}" class="btn btn-success"> Create Question</a></th>
                <form method="POST">
                    {% csrf_token %}
                    <input type="text" name="app" value="{{ request.META.HTTP_REFERER }}">
                <th><input type='submit' value='Add Selected'></th>
            {% for item in query %}
                <td> {{ item.category }} </td>
                <td> {{ item.question }} </td>
                <td> <input type="checkbox" name="selected_q" value="{{ }}"></td>
                <td> <a href="{% url 'JQ:delete_question' %}"> Delete </a> </td>
                <td> <a href="{% url 'JQ:edit_question' %}"> Edit </a> </td>
            {% endfor %}
{% endblock %}

Which is called from

class QuestionView(TemplateView):
    template_name = 'question.html'

    def get(self, request):
        query = Questions.objects.all().order_by('category')
        args = {'query': query}
        return render(request, self.template_name, args)

    def post(self, request):
        id_list = request.POST.getlist('selected_q')
        return_url = request.POST.get('app')
        app = int(return_url.rsplit('/')[-1])
        myApp = Application.objects.get(pk=app)
        if id_list != None:
            for item in id_list:
                myQuestion = Questions.objects.get(pk=int(item))
            return HttpResponseRedirect(return_url)
            query = Questions.objects.all().order_by('category')
            args = {'query': query}
            return render(request, self.template_name, args)

The user can only get to the questions page by going through the Application view and when that happens Questions.html captures the link that the user came from. Users then select one or more questions through a checkbox on questions.html which is captured in the post as id_list - So this should give me the two keys I need to create an instance of Ask.

However, when trying to do this it looks like everything works properly, but no instance of Ask is created and I don't get any error messages. I have tried creating a record from the python shell and it works just fine. I have also tried other methods such as using a form and instantiating Ask to call .save() afterwards, but it does not insert a record. For testing, I took out the query in to check to make sure valid values are getting passed to myApp and myQuestion, so I believe that the data is all there and accurate, but it isn't creating a record for some reason.

I appreciate any help you might offer, or maybe just different ideas on how to debug this problem so I can get some error messages.

1 answer

  • answered 2017-11-15 04:28 Rick Mooney

    Blurb was right - wrapping the table in the form tag resolved the issue. Thanks!