Re: url not redirecting?

14 views
Skip to first unread message
Message has been deleted

Gil Obradors

unread,
Apr 21, 2019, 2:45:30 PM4/21/19
to django...@googlegroups.com
Hi!
You have two entrys with same name

path('', views.post_new, name='new_post'),
    path
('', TemplateView.as_view(template_name='new_post.html'), name='new_post'),

Try to change one


Regards

El dg., 21 d’abr. 2019, 16:18, <silverst...@gmail.com> va escriure:
Hello, i'm attempting to redirect to a 'new post' page when someone clicks 'New Post'. Problem is, it redirects back to the public_posts page. the url in my HTML is {%  url 'new_post' %} but when i inspect the element in my browser, it says the href=/public_posts/ and no matter what I do, I can't get it to redirect/link correctly.


RELEVANT FILES

#public_posts/forms.py



from django import forms
from .models import Post

class PublicPostForm(forms.ModelForm):

   
class Meta:
        model
= Post
        fields
= ('title', 'text',)



#public_posts/urls.py


from django.urls import path
from django.conf.urls import url
from . import views
from django.views.generic import TemplateView

urlpatterns
= [
    path
('', views.pub_post_list, name='pub_posts'),
    path
('', views.post_new, name='new_post'),
    path
('', TemplateView.as_view(template_name='new_post.html'), name='new_post'),
    path
('', TemplateView.as_view(template_name='pub_posts.html'), name='pub_posts'),
]



#public_posts/views.py


from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.urls import reverse_lazy
from django.views import generic
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import PublicPostForm

# Create your views here.

def pub_post_list(request):
   
    posts
= Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
   
return render(request, 'public_posts/pub_posts.html', {'posts': posts})

def post_new(request):
    form
= PublicPostForm()
    posts
= Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')

   
#if this is a POST request we need to process the form data

   
if request.method == 'POST':
       
# create a form instance and populate it with data from the request:
        form
= PublicPostForm(request.POST)
       
# check whether it's valid:
       
if form.is_valid():
           
# process the data in form.cleaned_data as required
            post
.publish()
            post
.save()
           
           
# redirect to a new URL:
#            return HttpResponseRedirect('/new_post/') Commented out to try render()
           
return render(request, 'public_posts/new_post.html', {'form': form})

   
# if a GET (or any other method) we'll create a blank form
   
else:
        form
= PublicPostForm()
   
return render(request,  'public_posts/new_post.html', {'posts': posts})




#public_posts/models.py


from django.conf import settings
from django.db import models
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth import get_user_model


User = get_user_model()


class Post(models.Model):
    author
= models.ForeignKey(User, related_name='pub_poster', on_delete=models.CASCADE)
    title
= models.CharField(max_length=200)
    text
= models.TextField()
    created_date
= models.DateTimeField(default=timezone.now)
    published_date
= models.DateTimeField(blank=True, null=True)

   
def publish(self):
       
self.published_date = timezone.now()
       
self.save()

   
def __str__(self):
       
return self.title

URLCONFIG

from django.contrib import admin
from django.urls import path, include, resolve
from django.conf.urls import url
from admin_posts.views import home
from public_posts.views import post_new, pub_post_list
from django.views.generic.base import TemplateView

urlpatterns
= [
    path
('admin/', admin.site.urls),
    path
('', home, name='home'),
    path
('admin_posts/', include('admin_posts.urls')),
    path
('', pub_post_list, name='pub_posts'),
    path
('', post_new, name='post_new'),
    path
('public_posts/', include('public_posts.urls')),
    url
(r'^accounts/', include('allauth.urls')),
    path
('users/', include('users.urls')),

]





REVELANT HTML



<li class="new_post">
               
<h4><a href="{% url 'new_post' %}">New Post</a></h4>
           
</li>







--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/223a713a-d980-4e98-8b3a-ef58553a3ec1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Victor H. Velasquez Rizo

unread,
Apr 21, 2019, 2:48:09 PM4/21/19
to django...@googlegroups.com
Hello Silver.

- On your URLCONFIG add 
path('', include('public_posts.urls')),    <=== Add

- On your  public_posts/urls.py add
app_name = 'public_posts'
urlpatterns = [
    path
('', views.pub_post_list, name='pub_posts'),
    path
('', views.post_new, name='new_post'),

    path
('new-post/', TemplateView.as_view(template_name='new_post.html'), name='new_post'),   <=== Change

    path
('', TemplateView.as_view(template_name='pub_posts.html'), name='pub_posts'),
]

- On your HTML add
<h4><a href="{% url 'public_posts:new_post' %}">New Post</a></h4>    <=== Change

try and let me know if that works.

--



Atte...,.
Vìctor Hugo Velàsquez Rizo
Cali - Colombia

silverst...@gmail.com

unread,
Apr 21, 2019, 2:50:43 PM4/21/19
to Django users
Didnt't work, changed to: (pub_posts
urlpatterns = [
    path
('', views.pub_post_list, name='pub_posts'),

    path
('', views.post_new, name='post_new'),

    path
('', TemplateView.as_view(template_name='new_post.html'), name='new_post'),
    path
('', TemplateView.as_view(template_name='pub_posts.html'), name='pub_posts'),
]

Regards

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

silverst...@gmail.com

unread,
Apr 21, 2019, 2:53:07 PM4/21/19
to Django users
Forgot to add that one of the pub_posts is now public_posts, still doesn't work but thanks for pointing out the fact that it is probably pretty stupid to have more than one thing with the same name, probably saved me a good amount of future headaches.
Message has been deleted

silverst...@gmail.com

unread,
Apr 21, 2019, 3:06:05 PM4/21/19
to Django users
It took away my public_posts page and replaced it for my home page.
Message has been deleted

silverst...@gmail.com

unread,
Apr 21, 2019, 5:20:02 PM4/21/19
to Django users
I fixed it, none of the suggestions worked.
Reply all
Reply to author
Forward
0 new messages