简体   繁体   中英

Django create instance from views.py 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'. Models.py:

class Application(models.Model):
    status_choices = (
        ('NS','Not Started'),
        ('IP','In Progress'),
        ('OH','On Hold'),
        ('CO','Complete'),
    )
    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%">
            <tr>
                <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>
            </tr>
            {% for item in query %}
            <tr>
                <td> {{ item.category }} </td>
                <td> {{ item.question }} </td>
                <td> <input type="checkbox" name="selected_q" value="{{ item.id }}"></td>
                <td> <a href="{% url 'JQ:delete_question' item.id %}"> Delete </a> </td>
                <td> <a href="{% url 'JQ:edit_question' item.id %}"> Edit </a> </td>
            </tr>
            </form>
            {% endfor %}
        </table>
    </div>
{% endblock %}

Which is called from views.py:

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))
                Ask.objects.create(app=myApp,question=myQuestion)
            return HttpResponseRedirect(return_url)
        else:
            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 views.py 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.

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

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