Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easy way:</p> <p>If a model has a get_absolute_url function, the admin 'change' pages contain a 'view on site' button on the top right of the page. So make sure your Model contains get_absolute_url:</p> <pre><code> def get_absolute_url(self): return '/myapp/%s' %self.slug #this should reflect your url-structure. </code></pre> <p>The 'view on site' button is not on the 'add' page because the object needs to be created before you can visit it. Tell your client when he/she is creating a new object to NOT hit 'save' but 'save and continue editing' and than click on 'view on site'.</p> <p>Now it's also easy to jump to the website representation of the current model entry by overriding the base_site.html. Here we check if the current model contains absolute_url. If you are not on a change_page, there won't be a absolute_url. In this case the link takes you to the homepage. </p> <p>templates/admin/base_site.html</p> <pre><code>{% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %} &lt;h1 id="site-name"&gt;{% if has_absolute_url %}&lt;a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink"&gt;{% else %}&lt;a href="http://{{ site.domain }}"&gt;{% endif %}{{ site.domain }}&lt;/a&gt; Sitebeheer{#{% trans 'Django administration' %}#}&lt;/h1&gt; {% endblock %} {% block nav-global %}{% endblock %} </code></pre> <p>Done!</p> <p>The following solution does also answer your question but in a different way. This will add a 'save and view on site' button next to 'save and continue' and 'save' buttons on the admin 'add' and 'change' pages:</p> <ol> <li>Make sure your model contains a get_absolute_url function.</li> <li>Override the admin response to redirect to this absolute_url.</li> <li>Override change_form.html to add a 'save and view on site' button.</li> </ol> <p>Edit myproject/myapp/models.py</p> <pre><code>class MyModel(models.Model): title = models.CharField("titel", max_length=200) slug = models.SlugField(unique=True) def __unicode__(self): return self.title def get_absolute_url(self): return '/myapp/%s' %self.slug </code></pre> <p>Edit myproject/myapp/admin.py</p> <pre><code>from models import MyModel class MyModelAdmin(admin.ModelAdmin): ... def response_change(self, request, obj): """ Determines the HttpResponse for the change_view stage. """ if request.POST.has_key("_viewsite"): msg = (_('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(obj._meta.verbose_name), 'obj': force_unicode(obj)}) return HttpResponseRedirect(obj.get_absolute_url()) return super(MyModel, self).response_change(request, obj) admin.site.register(MyModel, MyModelAdmin) </code></pre> <p>Create a new file myproject/templates/admin/myapp/change_form.html:</p> <pre><code>{% extends "admin/change_form.html" %} {% load i18n %} {% block content %} {{ block.super }} &lt;script type="text/javascript"&gt;//&lt;![CDATA[ (function($){ $('&lt;input type="submit" value="Save and view on site" name="_viewsite" /&gt;') .prependTo('div.submit-row'); })(django.jQuery); //]]&gt;&lt;/script&gt; {% endblock %} </code></pre> <p>The template will override the change_form for each model in the myapp. This could be undesirable in your situation because not all models have a representation in the website (don't have and don't need get_absolute_url). I think you can also put the template at myproject/templates/admin/myapp/<strong>MyModel</strong>/change_form.html to only override the template for MyModel and leave other models in myapp with de default template. I never used a change_form template for a single model. Will you let me know if it worked?</p> <p>A big thank you for: <a href="http://djangosnippets.org/snippets/2005/" rel="nofollow">http://djangosnippets.org/snippets/2005/</a></p> <p>Did this answer your question? I hope it helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload