我想将人们重定向到他们刚提交后创建的最新文章。
网址不使用id而是slugs我该怎么办?我在考虑这样的事情:
views.py
// If the form is submitted, INSERT into table.
if (isset($_POST["submit"])) {
// Define $username and $password.
$username = $_POST['user_username'];
$password = $_POST['user_password'];
// Protect them from MySQL injection.
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
// Run some queries.
if ($_FILES["user_image"]["error"] > 0) {
//Bad Output for form results red text
echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
} else {
move_uploaded_file($_FILES["user_image"]["tmp_name"],"uploads/" . $_FILES["user_image"]);
$file="uploads/".$_FILES["user_image"];
$image_title = addslashes($_REQUEST['user_image']);
$sql="INSERT INTO users (user_fname, user_lname, user_image, user_phone, user_cell, user_email, user_username, user_password) VALUES ('$_POST[user_fname]', '$_POST[user_lname]', '$_POST[user_image]', '$_POST[user_phone]', '$_POST[user_cell]', '$_POST[user_email]', '$username', '$password')";
if ($mysqli->query($sql)) {
die('Error: ' . $mysqli->error);
}
//Good Output for form results green text
echo '
<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
<div style="padding:10px;">
<h2 style="font-size: 28px;">Success!</h2>
<p style="font-size: 18px;">Your file has been successfully uploaded!</p>
</div>
</form>';
}
}
为了进行此重定向有任何建议吗?
----更新----
文章/ urls.py
def article_id(request, slug):
context = {'article_id': get_object_or_404(Article, slug=slug)}
return render(request, 'article_id.html', context)
@login_required(login_url='/user')
def article_ajouter(request):
if request.method == 'POST':
form = ArticleForm(request.POST, request.FILES)
if form.is_valid():
save_it = form.save()
save_it.user = request.user
save_it.save()
object = Article.objects.get(article_id)
redirect(object, permanent=True) #I don't know if I am completely mistaken.
else:
form = ArticleForm()
context = {'form':form}
return render(request, 'article_ajouter.html', context)
文章/ views.py
url(r'^(?P<slug>[-\w\d]+)/$', views.article_id, name="article_detail"),
答案 0 :(得分:2)
您没有定义名为article_id
的局部变量,因此此代码会失败,因为它会尝试将具有该名称的视图传递给Article.objects.get()
,这根本没有意义。
但是没有必要进行任何查询; save_it
已经是您想要的文章了。如果Article类定义了get_absolute_url
,则此代码将起作用(只需更改一次,见下文);如果没有,那么您需要在重定向调用中使用URL模式名称。
最后一项更改是您需要实际返回重定向,就像您返回任何其他类型的响应一样。
因此,假设您已为article_id视图定义了如下所示的URL:
url(r'^articles/(?P<slug>\w+)/$', article_id, name='article_detail'),
然后你会这样做:
return redirect('article_detail', slug=save_it.slug)
(我还要指出“article_id”是一个奇怪的视图名称;无论如何它可能都想被称为“article_detail”。)
答案 1 :(得分:0)
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
def article_id(request, slug):
article_id = Article.objects.get(slug__iexact=slug)
context = {'article_id': article_id}
return render(request, 'article_id.html', context)
@login_required(login_url='/user')
def article_ajouter(request):
if request.method == 'POST':
form = ArticleForm(request.POST, request.FILES)
if form.is_valid():
save_it = form.save(commit=False)
save_it.user = request.user
save_it.save()
return HttpResponseRedirect(reverse('article_detail', args=(save_it.slug,)))
else:
form = ArticleForm()
context = {'form':form}
return render(request, 'article_ajouter.html', context)
这将是您的网址
url(r'^articles/(?P<slug>[\w-]+)/$', views.article_id, name='article_detail'),
如果有任何其他错误,这应该可以正常工作。请通知