开发者

django url from another template than the one associated with the view-function

开发者 https://www.devze.com 2023-01-01 18:09 出处:网络
Heyy there, i have for example a view function like this: def profile_view(request, id): u = UserProfile.objects.get(pk=id)

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 %}
  &l开发者_JAVA技巧t;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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号