--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.titleURLCONFIG
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.
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.