Advertisement
lamorfini

Untitled

May 19th, 2022
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. """
  2. Listato per risolvere il problema:
  3. AttributeError at /edit_profile/
  4. 'function' object has no attribute 'DoesNotExist'
  5. della funzione edit_profile
  6. """
  7.  
  8.  
  9. """
  10. models.py
  11. """
  12. class Profile(models.Model):
  13.     id = models.AutoField(primary_key=True)
  14.     user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
  15.     image = models.ImageField(upload_to="profile_pics", blank=True, null=True)
  16.     bio = models.TextField(blank=True, null=True)
  17.     phone_no = models.IntegerField(blank=True, null=True)
  18.     facebook = models.CharField(max_length=300, blank=True, null=True)
  19.     instagram = models.CharField(max_length=300, blank=True, null=True)
  20.     linkedin = models.CharField(max_length=300, blank=True, null=True)
  21.  
  22.     def __str__(self):
  23.         return str(self.user)
  24.  
  25. """
  26. views.py
  27. """
  28. def edit_profile(request):
  29.     try:
  30.         profile = request.user.profile
  31.     except Profile.DoesNotExist:
  32.         profile = Profile(user=request.user)
  33.     if request.method=="POST":
  34.         form = ProfileForm(data=request.POST, files=request.FILES, instance=profile)
  35.         if form.is_valid():
  36.             form.save()
  37.             alert = True
  38.             return render(request, "edit_profile.html", {'alert':alert})
  39.     else:
  40.         form=ProfileForm(instance=profile)
  41.     return render(request, "edit_profile.html", {'form':form})
  42.  
  43. """
  44. urls.py
  45. """
  46. from django.urls import path
  47. from . import views
  48. from .views import UpdatePostView
  49.  
  50. urlpatterns = [
  51. #     blogs
  52.     path("", views.blogs, name="blogs"),
  53.     path("blog/<str:slug>/", views.blogs_comments, name="blogs_comments"),
  54.     path("add_blogs/", views.add_blogs, name="add_blogs"),
  55.     path("edit_blog_post/<str:slug>/", UpdatePostView.as_view(), name="edit_blog_post"),
  56.     path("delete_blog_post/<str:slug>/", views.Delete_Blog_Post, name="delete_blog_post"),
  57.     path("search/", views.search, name="search"),
  58.  
  59.  
  60.  
  61. #    user authentication
  62.     path("register/", views.Register, name="register"),
  63.     path("login/", views.Login, name="login"),
  64.     path("logout/", views.Logout, name="logout"),
  65.  
  66.  
  67. #     profile
  68.     path("profile/", views.Profile, name="profile"),
  69.     path("user_profile/<int:myid>/", views.user_profile, name="user_profile"),
  70.     path("edit_profile/", views.edit_profile, name="edit_profile"),
  71.  
  72. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement