1

I'm trying to implement a SPA in my existing Django project with Angularjs. I have setup the start of a API using Django Rest Framework to retrieve my data in angular.

Some of my django model forms have a lot of complexity in them and would love to somehow pass it to the client side.

I could be wrong here but since I'm setting this project up as a SPA, once I send the user to the project from my django urls.py, then the angular routing via $routeProvider take over and talk to all my angular stuff to get data (controllers, services, etc.,) via the REST api. This then prevents me from sending my form over via context.

Every example I've seen with angularjs and django forms always explicitly writes out each field in the form. Is there anyway for me to get my django model form that has a bunch of code in its __init__ method, over to the client side without having to set each field manually?

1

1 Answer 1

0

I've never tried this, but here's how I'd start on trying to do it.

You can use the in-built as_p(), as_ul() and as_table() methods of the form to pass the form HTML from the REST API, e.g.

class MyApiView(ApiView):
    def get(self, request, format=None):
        form = MyForm()
        response['form_html'] = my_form.as_ul()

        return Response(response)

You would then need to build the form skeleton and submit buttons in Angular, then insert form_html as the form body just as you would do in a standard django template.

Disclaimer: It's been a while since I've properly used django-rest-framework so the syntax in my example mught not be correct, but the basic idea is there

Sign up to request clarification or add additional context in comments.

1 Comment

That gave me some legs to run with but unfortuantly I ran into a roadblock with angular rules with HTML noted here: stackoverflow.com/questions/11296193/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.