Thursday, 14 March 2019

django 17 ModelForm and CreateView



#music/models

from django.db import models
from django.urls import reverse

class Album(models.Model):
    artist = models.CharField(max_length=50)
    album_title = models.CharField(max_length=50)
    genre = models.CharField(max_length=50)
    album_logo = models.CharField(max_length=500)

    #form with no action redirect to detail
    def get_absolute_url(self):
        return reverse('music:detail', kwargs={'pk': self.pk})

    #query album.objects.get(pk=1)
    def __str__(self):
        return self.album_title + ' - ' + self.artist

class Song(models.Model):
    album = models.ForeignKey(Album, on_delete=models.CASCADE)
    file_type = models.CharField(max_length=50)
    song_title = models.CharField(max_length=50)
    is_favorite = models.BooleanField(default=False)

    def __str__(self):
        return self.song_title

-------------------------------------

#music/urls

from django.urls import path
from . import views

app_name = 'music'

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),

    #/music/id/
    path('<pk>/', views.DetailView.as_view(), name='detail'),

    #music/album/add/
    path('album/add/', views.AlbumCreate.as_view(), name='album-add')
]

---------------------------------------
#music/views

from django.views import generic
from .models import Album
from django.views.generic.edit import  CreateView, UpdateView, DeleteView

class IndexView(generic.ListView):
    template_name = 'music/index.html'

    def get_queryset(self):
        return  Album.objects.all()

class DetailView(generic.DeleteView):
    model = Album
    template_name = 'music/detail.html'

class AlbumCreate(CreateView):
    model = Album
    fields = ['artist', 'album_title', 'genre', 'album_logo']

---------------------------------------------
#music/templates/music/album_form

{% extends 'music/base.html' %}
{% block title %}New Album{% endblock %}
{% block albums_active %}active{% endblock %}

{% block body %}
    <div class="container-fluid">

        <div class="row">

            <div class="col-sm-12 col-md-7">

                <div class="panel panel-default">

                    <div class="panel-body">

                        <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
                            {% csrf_token %}
                            {% include 'music/form-template.html' %}
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <button type="submit" class="btn btn-success" >Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}

------------------------------------
#music/templates/form-template

{% for field in form %}

    <div class="form-group">

        <div class="col-sm-offset-2 col-sm-10">

            <span class="text-danger small">{{ field.errors }}</span>
        </div>
        <label class="control-label col-sm-2">{{ field.label_tag }}</label>
        <div class="col-sm-10">{{ field }}</div>
    </div>
{% endfor %}

reference:
https://stackoverflow.com/questions/43139081/importerror-no-module-named-django-core-urlresolvers

No comments:

Post a Comment