How to use the Django admin template non my form

Asked

Viewed 207 times

0

I’m learning about Django’s admin site and my question is:

I have a modelForm and one of the fields is a foreignKey and would like the change, Edit and delete options as in the Django template (that "+" button). I read about the documentation and found an option that is inlineModelAdmin, but I don’t know if I’m doing it correctly because my template doesn’t appear the buttons, if anyone can help me thank you > Django 3.0 & Python3

py.models:

from django.db import models

class TypePoke(models.Model):
    name = models.CharField(max_length=100)
    url = models.URLField()
    
    def __str__(self):
        return self.name


class Pokemon(models.Model):
    name = models.CharField(max_length=100)
    size = models.PositiveSmallIntegerField(blank=True, null=True)
    order = models.PositiveSmallIntegerField(blank=True, null=True)
    typepoke = models.ForeignKey(TypePoke, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.name

admin py.:

from django.contrib import admin
from pokedb.models import *

class PokeInline(admin.StackedInline):
    model = Pokemon  
    save_on_top = True  

   
class TypePokeAdmin(admin.ModelAdmin):
    inlines = [
        PokeInline,
    ]

    save_on_top = True
 
    
admin.site.register(Pokemon)
admin.site.register(TypePoke, TypePokeAdmin)

the form:

{% extends 'admin/base_site.html' %}


{% block content %}
<form action="{% url 'pokedb:pokeform_post' %}" method="POST">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Save">
</form>

{% endblock content %}

the layout:

inserir a descrição da imagem aqui

1 answer

1


Live on admin.py put only the following.

from django.contrib import admin
from pokedb.models import *

admin.site.register(Pokemon)
admin.site.register(TypePoke)

Result, this is the right expected?

inserir a descrição da imagem aqui

  • Hey, buddy, you’re still on the same display. I guess I didn’t explain myself but what I want is to extend or share this template from your pro form.html image of my application. I was looking at the documentation and I found it very confusing, very detailed. In short what I want is this same behavior of the admin form (an edit and create button) for Foreign key in the form of my application. I don’t know how to do this at the code level, so I was looking to share this Django template.

  • 1

    Yes, I actually understood that you wanted in admin, as you shared the admin.py. file and to do it in admin, just that. If it’s not admin, why are you just sharing admin code? For the popup add you have here https://django-addanother.readthedocs.io/en/latest/usage.html and to add and add/edit https://django-addanother.readthedocs.io/en/latest/edit-related.html

  • Yes exactly this popup I want and was not finding! I will look. Very obliged!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.