views:

39

answers:

2

Heyy there, i have for example a view function like this:

def profile_view(request, id):
    u = UserProfile.objects.get(pk=id)
return render_to_response('profile/publicProfile.html', {
    'object_list': u,

    }, 
    context_instance=RequestContext(request)) 

and the url: url(r'^profile_view/(?P\d+)/$', profile_view, name='profile_view'),

my problem is that i want to use the function's url in another template too, for example in the search in blog template, where people see the posts of some persons, and they want to navigate to their profile. the search view may look like that:

def searchn(request):
query = request.GET.get('q', '')
if query:
    qset = (
        Q(post__iexact=query) 
            )
    results = New.objects.filter(qset).distinct()
else:
    results = []
return render_to_response('news/searchn.html',{        
'results': results,
'query': query},
context_instance=RequestContext(request))

and the template:

{% if query %}

Results for "{{ query|escape }}":

{% if results %}
  <ul>
  {% for object in results %}
    <li>{{ object.post }} <a href='../../accounts/profile_view/{{object.id}}/'>  {{ object.created_by }} </a> {{object.date}} </li> 
  {% endfor %}
  </ul>
{% else %}
  <p>No posts found</p>
{% endif %}

{% endif %}

there, in created_by, i'd like to put a link to my user profile, but the user profile view doesn't 'point' to this template. What shoul i do? Thanks!

+1  A: 

You usually don't call model or view functions from a template, you link to URLs which in turn be dispatched to view functions.

The MYYN
i see but i cannot 'include' a view function in a template. if my function view has a render_to_response ('something.html'), than i can use in that template variables from that view function. but what if i want to use variables from another function view that has another render_to_response how can i use this? (this meaning the variables of that second function view). thanks
dana
I'm not sure if I fully understand your problem. If you have a function F1 and a template T1, F1's variable can be accessed in T1. However if you have a T2 and you want to access variables from F1, that's not possible directly. But then, why not defining (or querying) the needed variables in F2 (which renders to T2) and then access them 'normally'?
The MYYN
A: 

Dana: I hope I understand your problem correctly, but if you want to use the data from two "functions", you could achieve the same method by creating a model that links the two data fields you are using together, using a foreign key.

So you'd have

class MyCustomModel(models.Model):
  profile = models.ForeignKey(Profile, unique=True)
  otherdata = models.CharField("Some other field", blank=True, null=True)

You could then call your view on this model.

Martin Eve
hmm.. nope. i've just edited my question, i think i did't explain correctly first time
dana