Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

django - GenericViews - and pushing model name in URL

using Django Generic CreateView - is it possible for me to pass a value into the CreateView from the URL call, that defines which model/table to base the view on?

I did try get_context_data but believe that is not the solution, as I think it only pushes it to the rendered template.

You will see from my scripts - I am pushing the value 'syslog_policy' - i would like the view to set the model variable to be, what-ever value I push from the URL.

The reason for this, I have some pages/models that are similar - so rather than create multiple html pages and views - I wouldn't need to if I could get this to work.

URL Call

<li><a href="{% url 'security_app:HardenTemplateCreate' 'syslog_policy' %}">Update/Delete Policies</a></li>

urls.py

path('HardenTemplateCreate/<str:element>', HardenTemplateCreate.as_view(success_url="/security_tooling/add_success") ,name="HardenTemplateCreate")

views.py

        
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['element']= self.kwargs['element']
        print(context['element'])
        return context

    model = !!<NEED VALUE SUPPLIED IN URL HERE>!!
    fields = ['name','template']
    template_name = 'security_app/add.html'```

question from:https://stackoverflow.com/questions/65894817/genericviews-and-pushing-model-name-in-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This would assume that all the models we are working with here belong to the same app (main) - if not, you also need to pass that to form kwargs and handle accordingly. In forms.py:

from django.apps import apps
from django.forms.models import fields_for_model

class VariableModelForm(forms.Form):

    def __init__(self, *args, **kwargs):
        model_name = kwargs.pop('model_name', None)
        model = apps.get_model(app_label='main', model_name=model_name)
        model_fields = fields_for_model(model)
        super(VariableForm, self).__init__(*args, **kwargs)
        for field in model_fields:
            self.fields[field] = model_fields[field]

In your CreateView:

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form']= VariableModelForm(model_name=self.kwargs['modelname'])
        return context

You grab the model name from the url kwargs, pass it to your form as an extra positional argument, and use it in your form's init method to set up the fields.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...