2

models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    security_question = models.CharField('Question', max_length=255, null=True, blank=True)
    security_answer = models.CharField('Answer', max_length=255, null=True, blank=True)
    phone_daytime = models.CharField('Phone daytime', max_length=50, null=True, blank=True)
    phone_mobile = models.CharField('Phone mobile', max_length=50, null=True, blank=True)
    work_street = models.CharField('Street', max_length=255, null=True, blank=True)
    work_suburb = models.CharField('Suburb', max_length=200, null=True, blank=True)
    work_state = models.CharField('State', max_length=100, null=True, blank=True)
    work_postcode = models.CharField('Postcode', max_length=20, null=True, blank=True)
    work_country = models.CharField('Country', max_length=200, null=True, blank=True)

views.py

def method(request):
    ''''
    profile = UserProfile.objects.get(user=user)
    '''''
    return render(request,'index.html',{'profile':profile})

How to convert the database field's work_street,work_suburb,work_state,work_postcode, work_country into string and render it in template.

2
  • 1
    You don't have to. {{ profile.work_street }} will render it perfectly fine in your template. Commented Jul 22, 2013 at 13:35
  • @limelights The 4 field what i mentioned is the address of the work place,i want to pass that as string in javascript for fixing marker in position so i can do like this for mentioning address var address = "{{ profile.work_street }}{{profile.work_suburb}}{{profile.work_postcode}}"; is it right way if not please correct me how to do that. Commented Jul 22, 2013 at 13:44

1 Answer 1

2

you can create index.html like this to render it as html

index.html

<body>
<p>{{ profile.user.username }}</p>
<p>{{ profile.security_question }}</p>
<p>{{ profile.work_street}}</p>
<p>{{ profile.work_suburb}}</p>
<p>{{ profile.work_state}}</p>
<p>{{ profile.work_postcode}}</p>
<p>{{ profile.work_country }}</p>
..... etc

<script>
 var address = "{{ profile.work_street }}{{profile.work_suburb}}{{profile.work_postcode}}";
 alert(address);
</script>
</body>
Sign up to request clarification or add additional context in comments.

2 Comments

want to pass that as string in javascript for fixing marker in position so i can do like this for mentioning address var address = "{{ profile.work_street }}{{profile.work_suburb}}{{profile.work_postcode}}"; is it right way if not please correct me.
@Royal yes you can defenitily do that. that is the correct way. when page loads the string will store in address

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.