Skip to content
Snippets Groups Projects
Commit 80dde338 authored by Andrew Waterhouse's avatar Andrew Waterhouse
Browse files

Update data using ModelForm

parent 56d8f4f4
No related branches found
No related tags found
No related merge requests found
from django.db import models from django.db import models
from django import forms
from django.forms import ModelForm
# Create your models here. # Create your models here.
class Biounit(models.Model): class Biounit(models.Model):
...@@ -35,3 +37,17 @@ class Ligand(models.Model): ...@@ -35,3 +37,17 @@ class Ligand(models.Model):
('natural','Natural, non covalently bound'), ('natural','Natural, non covalently bound'),
('synthetic','Synthetic, non covalently bound')) ('synthetic','Synthetic, non covalently bound'))
) )
class BiounitForm(ModelForm):
class Meta:
model = Biounit
fields = ['title','resolution']
def clean_resolution(self):
res = self.cleaned_data['resolution']
if res<1 or res>3:
raise forms.ValidationError("Resolution must be between 1 and 3!")
# Always return the cleaned data, whether you have changed it or
# not.
return res
\ No newline at end of file
...@@ -10,5 +10,18 @@ ...@@ -10,5 +10,18 @@
<img src="http://swissmodel.expasy.org/templates/{{ biounit.smtl_id }}_s500.png"> <img src="http://swissmodel.expasy.org/templates/{{ biounit.smtl_id }}_s500.png">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Edit Title and Resolution</h3>
</div>
<div class="panel-body">
<form action="" method="post">
{% csrf_token %}
{{ form }}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from models import Biounit, Ligand from models import Biounit, Ligand, BiounitForm
# Create your views here. # Create your views here.
...@@ -15,4 +15,9 @@ def single_biounit(request, smtl_id): ...@@ -15,4 +15,9 @@ def single_biounit(request, smtl_id):
biounit = Biounit.objects.get(smtl_id=smtl_id) biounit = Biounit.objects.get(smtl_id=smtl_id)
return render(request, 'biounit.html', {'biounit':biounit}) form = BiounitForm(request.POST or None, instance=biounit)
if form.is_valid():
form.save()
return render(request, 'biounit.html', {'biounit':biounit, 'form':form})
...@@ -6,4 +6,13 @@ body,html{ ...@@ -6,4 +6,13 @@ body,html{
background-color: #0c4b33;padding:16px; background-color: #0c4b33;padding:16px;
color:white; color:white;
} }
img { max-width:100%;} img { max-width:100%;}
\ No newline at end of file
form label{
display:block;
}
.errorlist{
color:red;
font-weight:bold;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment