Monday, January 20, 2014

From the following model: class Sample (models.Model):


From the following model: class Sample (models.Model): "" "this is just a sample model" "" slug = models.SlugField (max_length = 50, unique = True) name = models.CharField (max_length kerala psc = 100) Ammounts models.IntegerField = () = models.TextField comments (blank = True, null = True) class Meta: verbose_name = 'Sample' verbose_name_plural = 'Sample' def __ unicode__ (self): return self.name
Django more than DetailView provides the following views already built for this type of tasks: * `` CreateView that manages the creation UpdateView * `` * `which manages update` handles deleted DeleteView
We called our class SampleCreateView so the amount defined in the views.py from django.views.generic.edit CreateView from main.models amount SampleCreateView class Sample (CreateView): model = Sample
This Django internally and we will try to submit a form and a team that builds the model name and suffix _format, ie, in our case sample_form.html going to set context variable form form
If we want to track: <form action = "." method = "post" accept-charset = "utf-8"> {% csrf_token <table>%} {{form}} </ table> <input <p> type="Submit" value="Save "> </ p> </ form>
If creau Put the template and you are trying to save and valid data will error "No URL to redirect to. Either Provide a URL or to define get_absolute_url method on the model." Slowly and now it will. Show me the object
Remember the bug mentioned above? So what we'll do is modify the model to define the `get_absolute_url, could also be defined or overwritten success_url kerala psc get_success_url but we saw this already, so it varies a little.
Modificam our model class Sample (models.Model): "" "this is just a sample model" "" slug = models.SlugField (max_length = 50, unique = True) name = models.CharField (max_length = 100) = Ammounts models.IntegerField () comments = models.TextField (blank = True, null = True) class Meta: verbose_name = 'Sample' verbose_name_plural = 'Sample' def __ unicode__ (self): return self.name def @ models.permalink get_absolute_url (self ): return ('tutorial_view_sample' [self.id,)]
We could also have done the same using the slug, thus would friendlier urls. So we must change both the url as get_absolute_url class Sample (models.Model): "" "this is just a sample model" "" slug = models.SlugField (max_length = 50, unique = True) name = models.CharField (max_length = 100) = Ammounts models.IntegerField () comments = models.TextField (blank = True, null = True) class Meta: verbose_name = 'Sample' verbose_name_plural = 'Sample' def __ unicode__ (self): return self.name @ models.permalink get_absolute_url def (self): kerala psc return ('tutorial_view_sample' [self.slug,])
The update is also very simple. We will use UpdateView but unlike the creation of the URL you've had a good primary key of the object or the slug (Sempra assuming that this is unique).
Now we need only link the URL to go to the editing of the object, we can do it directly from the navigation bar, but it is also best to put on the website, for example, a link to the template display that takes us to the issue of objecete. That matter will also link creation. kerala psc In sum, our sample_detail.html would be of the form: <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.1 / / EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11 . dtd "> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> kerala psc <title> test </ title> </ head> <body> < h1> Sample Detail </ h1>}} {{sample.slug <br/> sample.name}} {{{{<br/> sample.ammount <br/>}}}} {{sample.comments <br /> <a href="{% url tutorial_update_sample sample.slug %}"> Update </ a> | <a href="{% url tutorial_create_sample %}"> Create </ a> </ body> </ html> Elimination
We have reached the last letter of the acronym. To delete an object Django provides DeleteView view. Before deleting it convenient confirmation and implementation of Django has this in mind.
In this template we ask confirmitat to clear the record, sending it to the same URL but with a post deleted. That is the url with GET and POST has confirmed the action does. <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.1 / / EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns = "http:// www.w3.org/1999/xhtml "xml: lang =" en "> <head> <title> test </ title> </ head> Delete <body> <h1> Sample </ h1> {{sample.slug <br/> sample.name {{}}}} {{<br/> sample.ammount <br/>}}}} kerala psc {{sample.comments <br/> <p> Are you sure? </ p> <form action = "." method kerala psc = "post" accept-charset = "utf-8"> csrf_token%} {% <

No comments:

Post a Comment