19- Custom Add Actor Form

We have already created the AddMovieForm, now we need to createAddActorForm. AddActorForm also creates the relationship between Stars and Movies.

 Import Actor model to forms.py

forms.py

class AddActorForm(forms.ModelForm):
    actorname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
    #theform data is fetched from db and the SelectMultiple field is defined as 20
    movies = forms.ModelMultipleChoiceField(queryset=Movie.objects.all(), widget=SelectMultiple(attrs={'size':20}))
    class Meta:
        model = Actor
        fields = ('actorname', 'movies')

 

urls.py

path('addactor/', views.addactor, name='addactor'),

 

views.py

Import AddActorForm to views.py first, then create the view function

def addactor(request):
    if request.method=='POST':
        form = AddActorForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Actor is Added", extra_tags='green')
            return redirect('addactor')
    else:
        form = AddActorForm()
    context = {'form':form}
    return render(request, 'addactor.html', context)

 

create a template named addactor.html

addactor.html

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title %}
<title>Add Movie</title>
{% endblock title%}

{% block content %}
<div class="row justify-content-center">
        <div class="col-10">
            <div class="card">
                <div class="card-body">
				    <div class="col-md-10 offset-md-2">
                        <h2 class="text-center">Add An Actor</h2>
                        <!--enctype="multipart/form-data" is needed here, 
                        because we have multiple filefields in a single form.-->
                        <form method="POST" action="{% url 'addactor' %}">
                        {% csrf_token %}
                        {% if form.errors %}
                            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                <strong>Your Form has errors</strong>
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            </div>
                        {% endif %}

                        <div class="row d-flex justify-content-center">
                            <div class="col-sm-4">
                            {{ form.actorname | as_crispy_field }}
                            {{ form.movies | as_crispy_field }}
                            <input type="submit" value="Add" class="btn btn-secondary mb-2">
                            </div>
                        </div>

                        </form>
					</div>                  
                </div>
            </div>
        </div>
</div>
{% endblock content%}

 

Add a link for this url on your nav-bar (base.html) just like we did for the addmovie url.

base.html

            {% if user.is_authenticated %}
            <ul class="navbar-nav" >
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle text-white" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                  {{user.username}}'s Profile
                </a>
                <ul class="dropdown-menu" style="background-color: #86aac4;">
                  <li><a class="dropdown-item text-white" href="#">My List</a></li>
                  <li><a class="dropdown-item text-white" href="/{% url 'editprofile' %}">My Account</a></li>
                  {% if user.is_authenticated and user.is_superuser%}
                  <li><a class="dropdown-item text-white" href="/{% url 'addmovie' %}">Add Movie</a></li>
                  <li><a class="dropdown-item text-white" href="/{% url 'addactor' %}">Add Actor</a></li>
                  {% endif %}
                  <li><hr class="dropdown-divider"></li>
                  <li><a class="dropdown-item text-white" href="/{% url 'logoutuser' %}">Log Out</a></li>
                </ul>
              </li>
              <li class="nav-item">
                <a class="nav-link disabled">Disabled</a>
              </li>
            </ul>

 

 

If I go and check these movies, I can see this actor is added for each of them.