0

In views.py

def website(request, pk=None):
if not model_utils.check_admin_permission(request): 
    return loginAndContinuosRequest(request)

if pk:
    obj = get_object_or_404(Website, pk = pk)
    form = WebsiteForm(instance=obj)
    fields = obj.get_manage_payment_accounts()
else:
    obj = None
    form = WebsiteForm()
    fields = []

if request.POST:
    if pk:
        form = WebsiteForm(request.POST, request.FILES, instance=obj)
    else:
        form = WebsiteForm(request.POST, request.FILES)

    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse("views_manage_websites"))

params = {
    'obj':obj ,
    'form':form,
    'fields': fields,
    'test': 'test',
}

return render(request, TEMPLATE_PATH + 'website.html', params)

In script in template (.html),

<script>

    $(document).ready(function() {
        alert({{ obj.id }}); //(1)
        alert({{ obj.pk }}); //(2)
        alert({{ obj.site_name }}); //(3)
        alert ({{ test }}); //(4)
    });

</script>

The alert (1) and (2) is shown. But the alert (3) and (4) isn't shown. Can you explain for me. Thanks.

1
  • Are you sure that the instance of the model that you are using have a value for that field (site_name)? Commented Dec 7, 2017 at 9:00

1 Answer 1

1

obj.site_name and test are strings. In JavaScript, as in Python, strings need to be surrounded by quotes.

    alert("{{ obj.site_name }}");
    alert("{{ test }}");

Your browser's developer tools will have shown the original code generating a syntax error.

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

Comments

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.