patternpythondjangoMinor
Am I being too redundant in this Django view (or can I reduce the repetition in my code)?
Viewed 0 times
thiscanthedjangoviewtoobeingredundantreducerepetition
Problem
I'm new to Django and learning my ways around it. I'm writing a fake basic CRUD app to get me started. In the following code I pretty much copy and paste the same code four times with minor adjustments between each. I only pasted it here twice for the Bands and Album models.
Am I doing this right?
```
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from models import Band, Album, Song, Instrument
from forms import BandForm, AlbumForm
# Bands Functions:
def bands_index(request):
latest_band_list = Band.objects.all().order_by('-added_on')[:5]
return render(request,
"bands/index.html", {
'latest_band_list': latest_band_list
})
def band_detail(request, band_id):
b = get_object_or_404(Band, pk=band_id)
return render(request, 'bands/detail.html', {'band': b})
@login_required
def band_add(request):
form = BandForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
instance = form.save(commit=False)
instance.added_by = request.user
instance.save()
return redirect('/bands/')
else:
return render(request, 'bands/add.html',
{'form': form},
context_instance=RequestContext(request))
@login_required
def band_remove(request, band_id):
b = get_object_or_404(Band, pk=band_id)
b.delete()
return redirect('/bands/')
@login_required
def band_edit(request, band_id):
b = get_object_or_404(Band, pk=band_id)
form = BandForm(request.POST or None, instance=b)
if request.method == 'POST' and form.is_valid():
instance = form.save(commit=False)
instance.save()
return(redirect('/bands/'))
else:
return render(request, 'bands/edit.html',
{'form': form, 'band_id': band_id},
context_instance=RequestContext(request))
# Albu
Am I doing this right?
```
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from models import Band, Album, Song, Instrument
from forms import BandForm, AlbumForm
# Bands Functions:
def bands_index(request):
latest_band_list = Band.objects.all().order_by('-added_on')[:5]
return render(request,
"bands/index.html", {
'latest_band_list': latest_band_list
})
def band_detail(request, band_id):
b = get_object_or_404(Band, pk=band_id)
return render(request, 'bands/detail.html', {'band': b})
@login_required
def band_add(request):
form = BandForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
instance = form.save(commit=False)
instance.added_by = request.user
instance.save()
return redirect('/bands/')
else:
return render(request, 'bands/add.html',
{'form': form},
context_instance=RequestContext(request))
@login_required
def band_remove(request, band_id):
b = get_object_or_404(Band, pk=band_id)
b.delete()
return redirect('/bands/')
@login_required
def band_edit(request, band_id):
b = get_object_or_404(Band, pk=band_id)
form = BandForm(request.POST or None, instance=b)
if request.method == 'POST' and form.is_valid():
instance = form.save(commit=False)
instance.save()
return(redirect('/bands/'))
else:
return render(request, 'bands/edit.html',
{'form': form, 'band_id': band_id},
context_instance=RequestContext(request))
# Albu
Solution
I'd say the best way to be "less redundant" would be to use the already-existing generic views - e.g.
(I assume you're looking for "the right way to do things" rather than "I'm reinventing the wheel to learn"...)
DetailView instead of your band_detail.(I assume you're looking for "the right way to do things" rather than "I'm reinventing the wheel to learn"...)
Context
StackExchange Code Review Q#10389, answer score: 2
Revisions (0)
No revisions yet.