4

I want show two button in same form, first button I want use for delete object, and second button to create an object.

For example i want create simple Model like:

models.py:

class UrlStatus_Proxy(models.Model):

    urls = models.URLField(u'Site URL', max_length=100, null=True, unique=True)
    status_url = models.CharField(u'Site', max_length=20, choices=STATUS_URL)

urls.py

url(r'^url_status/$',ProxyUrlCreateView.as_view(model=UrlStatus_Proxy,
     get_success_url=lambda: reverse('proxy_url_status'),template_name='proxy_url_status.html'), name='proxy_url_status'),

proxy_url_status.html

<form action="" method="post">    
    {{form.as_p}}
    <input type="submit" name="delete" id="delete">
    <input type="submit" name="add" id="add">
</form>

If I don't have objects in database then do nothing, just displayed form from Model, and you have just one option to add new object in database.

If I have objects in database then list object like table and in table I have one checkbox field. When I checked one of the object and click button "delete" i want delete that object.

In second case If I fill input field from object and press button "add", I want add object in base.

How Can I do it?

WBAR
  • 4,924
  • 7
  • 47
  • 81
prog.Dusan
  • 1,324
  • 2
  • 12
  • 25

1 Answers1

3

First add all existing objects to the context of CreateView and update the HTML template to render these as a table above the form. Then create a DeleteView and map a URL to it.

URLs

url(r"^url_status/$",
    ProxyUrlCreateView.as_view(),
    name="proxy_url_status"),

url(r"^url_status/(?P<pk>\d+)/delete/?$",
    DeleteProxyURLView.as_view(),
    name="delete_proxy"),

Views

from django.views.generic import DeleteView
from django.core.urlresolvers import reverse

# add existing objects to the context, making them available to the template
class ProxyUrlCreateView(CreateView):
    model = UrlStatus_Proxy
    template_name = "proxy_url_status.html"

    def get_success_url(self):
        return reverse("proxy_url_status")

    def get_context_data(self, **kwargs):
        kwargs["object_list"] = UrlStatus_Proxy.objects.all()
        return super(ProxyUrlCreateView, self).get_context_data(**kwargs)

class DeleteProxyURLView(DeleteView):
    model = UrlStatus_Proxy

    def get_success_url(self):
        """
        Redirect to the page listing all of the proxy urls
        """
        return reverse("proxy_url_status")

    def get(self, *args, **kwargs):
        """
        This has been overriden because by default
        DeleteView doesn't work with GET requests
        """
        return self.delete(*args, **kwargs)

Template

<table>
    {% for proxy_url in object_list %}
    <tr>
        <td>{{ proxy_url.urls }}</td>
        <td><a href="{% url delete_proxy %}">Delete</a></td>
    </tr>
    {% endfor %}
</table>

<form action="" method="post">    
    {{form.as_p}}
    <input type="submit" name="add" id="add">
</form>
Matt
  • 8,758
  • 4
  • 35
  • 64
  • Hmm.. Can u write me little bit of code, because I am new django programmer and I cant understand exactly what did you write me. – prog.Dusan Feb 27 '13 at 21:15
  • ok. I did, and now how can I create/delete object depends of which button I pressed? – prog.Dusan Feb 27 '13 at 21:22
  • Ah ok, I misunderstood the question. The code I've provided above will display a table of the existing objects with a ‘delete’ link alongside each one. – Matt Feb 27 '13 at 21:35
  • If you're still going down the checkbox delete button route I suggest either doing this client side with Javascript (with a view to handle deletion of multiple objects) or looking into model formsets in combination with the [extra views](http://git.io/W2Qfow) package. – Matt Feb 27 '13 at 21:52
  • Yess, I have jquery function for check all checkbox, but I don't know how can I implement submit button in that case. ;) – prog.Dusan Feb 27 '13 at 22:02