Commit 7d4195fc authored by Aatish Neupane's avatar Aatish Neupane

use crispy with bootstrap4 for forms, job creation view with autocomplete tags

parent d122a1db
......@@ -39,6 +39,18 @@ INSTALLED_APPS = [
# Libraries and other apps
# Used for autocomplete and dynamic creation of tags.
# Ff this widget is required for admin, place these
# before 'django.contrib.admin' app
# Our apps
......@@ -122,3 +134,6 @@ USE_TZ = True
STATIC_URL = '/static/'
from dal import autocomplete
from .models import Tag
class AuthenticatedUserCreateMixin():
''' By default, autocomplete-light only allows creation of
choices if the user has add permission on the model. However,
we need to allow any authenticated user to create tags
def has_add_permission(self, request):
print("i am called", request.user)
return request.user.is_authenticated
class TagAutocomplete(
AuthenticatedUserCreateMixin, autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated:
return Tag.objects.none()
qs = Tag.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
return qs
\ No newline at end of file
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from dal.autocomplete import ModelSelect2Multiple
from .models import Job
class JobForm(forms.ModelForm):
class Meta:
model = Job
fields = ('title', 'description', 'tags')
widgets = {
'tags': ModelSelect2Multiple(url='jobs:tag-autocomplete')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Post Job'))
......@@ -9,6 +9,9 @@ User = get_user_model()
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True, db_index=True)
class Meta:
ordering = ['name']
def __str__(self):
......@@ -18,7 +18,7 @@
<a class="p-2 text-dark" href="#">Tags</a>
<a class="p-2 text-dark" href="#">Jobs</a>
<a class="btn btn-outline-primary" href="#">Login</a>
<a class="btn btn-outline-primary" href="{% url 'jobs:create' %}">Post a job</a>
<div class="container">
{% extends 'base.html' %}
{% block title %}Post a Job{% endblock %}
{% load crispy_forms_tags %}
{% block body_content %}
<div class="row">
<div class="col-md-12">
{% crispy form %}
{% endblock %}
\ No newline at end of file
from django.urls import path
from . import views
from . import autocomplete as autocomplete_views
app_name = 'jobs'
urlpatterns = [
path('', views.Index.as_view(), name='index'),
path('jobs/', views.JobList.as_view(), name='jobs'),
path('jobs/create/', views.JobCreate.as_view(), name='create'),
path('jobs/', views.JobList.as_view(), name='list'),
# autocomplete endpoints
urlpatterns += [
\ No newline at end of file
from django.shortcuts import render
from django.views.generic import TemplateView, ListView
from django.urls import reverse_lazy
from django.views.generic import (
TemplateView, ListView, CreateView
from .models import Job
from .forms import JobForm
class Index(TemplateView):
template_name = 'jobs/index.html'
......@@ -11,3 +15,13 @@ class JobList(ListView):
context_object_name = 'jobs'
model = Job
class JobCreate(CreateView):
model = Job
form_class = JobForm
success_url = reverse_lazy("jobs:list")
def form_valid(self, form):
obj =
obj.posted_by = self.request.user
return super().form_valid(form)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment