0
votes

I need help with my code. I have read through the code several times and I didn't see anything wrong with it. The user is expected to submit a job application and redirect the user to the dashboard, but it did not submit the job application neither does it direct the user to the dashboard. here is my code: mode.py

from django.db import models
from django.contrib.auth.models import User


class Job(models.Model):
    title = models.CharField(max_length=255)
    short_description = models.TextField()
    long_description = models.TextField(blank=True, null=True)


    created_by = models.ForeignKey(User, related_name='jobs', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    changed_at = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.title

class Application(models.Model):
    job = models.ForeignKey(Job, related_name='applications', on_delete=models.CASCADE)
    content = models.TextField()
    experience = models.TextField()

    created_by = models.ForeignKey(User, related_name='applications', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

Views.py

from django.shortcuts import render, redirect

from django.contrib.auth.decorators import login_required

from .forms import AddJobForm, ApplicationForm from .models import Job

def job_detail(request, job_id): job = Job.objects.get(pk=job_id) return render(request, 'jobs/job_detail.html', {'job': job})

@login_required def add_job(request): if request.method == 'POST': form = AddJobForm(request.POST)

    if form.is_valid():
        job = form.save(commit=False)
        job.created_by = request.user
        job.save()

        return redirect('dashboard')

else:
    form = AddJobForm()

return render(request, 'jobs/add_job.html', {'form': form})

@login_required def apply_for_job(request, job_id): job = Job.objects.get(pk=job_id)

if request.method == 'POST':
    form = ApplicationForm(request.POST)

    if form.is_valid():
        application = form.save(commit=False)
        application.job = job
        application.created_by = request.user
        application.save()

        #create_notification(request, job.created_by, 'application', extra_id=application.id)

        return redirect('dashboard')
else:
    form = ApplicationForm()

return render(request, 'jobs/apply_for_job.html', {'form': form, 'job': job})

forms.py

from django import forms

from .models import Job, Application

class AddJobForm(forms.ModelForm): class Meta: model = Job fields = ['title','short_description','long_description']

class ApplicationForm(forms.ModelForm): class Meta: model = Application fields = ['content', 'experience']

apply_for_job.html

{% extends 'core/base.html' %}

{% block content %} Apply for job - {{ job.title }}

<form method="post" action=".">
    {% csrf_token %}

    {% if form.errors %}
        {% for error in form.errors %}
            <div class="notification is-danger">
                {{ error }}
            </div>
        {% endfor %}
    {% endif %}

    <div class="field">
        <label>Content</label>

        <div class="control">
            <textarea class="textarea" name="content" id="id_content"></textarea>
        </div>
    </div>

    <div class="field">
        <label>Experience</label>

        <div class="control">
            <textarea class="textarea" name="experience" id="id_experience"></textarea>
        </div>
    </div>

    <div class="field">
        <div class="control">
            <button class="button is-success">Submit</button>
        </div>
    </div>
</form>

{% endblock %}

what could be wrong with my code?

1
Its difficult to tell where the problem is from exactly, but your Application Form certainly is not validating. If you made any changes to your models at anytime, confirm from your database that all changes were effected. Otherwise, you may need to delete all migrations files, delete database, make migrations, migrate, and recreate superuser, then try again.Seyi Daniel
You should also print the data from your Application Form to console before validating, so you could see if any of the form fields sends an invalid data.Seyi Daniel

1 Answers

0
votes

the error in this line

<form method="post" action=".">

just delete the point, and it should work,

<form method="post" action="">