Yuksel Bastan
Yuksel Bastan

Reputation: 16

template code not working in different app template

I try to learn Django, I'm sure that my question is stupid but I think every beginner must pass this route to success, of course with the help of pro's.

Also, I have a site with 2 app's, profiles and posts In this project I can see who I'm following in a list in template (posts/main.html) the problem is that I need the same list also in the other template (profiles/main.html).

I have made a copy of the piece of code but it doesn't worked. I really don't know how to explain it better or I don't know how I can search it exactly. I tried this : 1.) How to use views in different apps templates 2.) How to import views from other apps views 3.) and a lot more, I read all the stuff but I couldn't find what I'm looking for

Thanks a lot for your help.

POSTS APP

posts/models.py

class Post(models.Model):
    ...
    author = models.ForeignKey(Profile, on_delete=models.CASCADE)
    text = models.TextField(default='no text...')
    ...
    def __str__(self):
        return str(self.text)[:30]

posts/urls.py

from django.urls import path
from .views import posts_of_following_profiles, index, contact, about, category, PostListView, PostDetailView, post, detay
from profiles.views import ProfileListView
app_name="posts"

urlpatterns = [
    ...
    path('following/', ProfileListView.as_view(), name='posts-follow-view'),
    ...
    ]

posts/views.py

def posts_of_following_profiles(request):
    
    # get logged in user profile
    profile = Profile.objects.get(user=request.user)

    # check who we are following
    users = [user for user in profile.following.all()]

    # initial values for variables
    posts = []
    qs = None

    # get the posts of people who we are following
    for u in users:
        p = Profile.objects.get(user=u)
        p_posts = p.post_set.all()
        posts.append(p_posts)
    
    # our posts
    my_posts = profile.profile_posts()
    posts.append(my_posts)

    # sort and chain qs and unpack the post list
    if len(posts)>0:
        qs = sorted(chain(*posts), reverse=True, key=lambda obj: obj.ilan_tarihi)


    return render(request, 'posts/main.html', {'profile':profile, 'posts':qs})

posts/main.html

THIS PART IS WORKING WELL

<h3>list of followings</h3>
{% for p in profiles.following.all %}
<br>{{p}} <br>
{% endfor %}

PROFILES APPS

profiles/models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    following = models.ManyToManyField(User, related_name='following', blank=True)
    text = models.TextField(default='no text...')
    
    def profile_posts(self):
        return self.post_set.all()

    def __str__(self):
        return str(self.user.username)  

profiles/urls.py

from django.urls import path
from .views import ProfileListView, ProfileDetailView, follow_unfollow_profile
from posts.views import posts_of_following_profiles

app_name = 'profiles'

urlpatterns = [
    path('switch_follow/', follow_unfollow_profile, name='follow-unfollow-view'),
    path('portfolio/', posts_of_following_profiles, name='profile-list-view'),
    path('<pk>/', ProfileDetailView.as_view(), name='profile-detail-view'),
]

profiles/views.py

def follow_unfollow_profile(request):
    if request.method=="POST":
        my_profile = Profile.objects.get(user=request.user)
        #profile_pk is the name of the input in detail.html
        pk = request.POST.get('profile_pk')
        obj = Profile.objects.get(pk=pk)

        if obj.user in my_profile.following.all():
            my_profile.following.remove(obj.user)
        else:
            my_profile.following.add(obj.user)
        return redirect(request.META.get('HTTP_REFERER'))
    return redirect('profiles:profile-list-view') 

profiles/main.html

THAT'S WHAT I WANT THIS CODE WILL NOT WORK (it's the same code as in the posts/main.html)

<h3>list of followings</h3>
{% for p in profiles.following.all %}
<br>{{p}} <br>
{% endfor %} 

 

Upvotes: 0

Views: 69

Answers (1)

mrd210
mrd210

Reputation: 21

In profiles/urls.py, even though you have imported follow_unfollow_profile(view). Try the below code:

path('switch_follow/', views.follow_unfollow_profile, name='follow-unfollow-profile'),

Pretty much this is the reason django is able to set a path but cannot follow the instructions mentioned in the views.py file.

Also make sure,

'DIRS': [TEMPLATE_DIR,],

is included in the root directory's settings.py file, under the TEMPLATES section.

Upvotes: 1

Related Questions