简体   繁体   中英

Django Submit Form Working Development Not Deployment

I have a form that creates a new blog post and redirects me to that blog post. The form works properly in development but when I deployed the app to Heroku and click the submit button nothing happens. Is something wrong with the database, my form, or the admin functionality (which is required to access the form)? In terms of the database (in case it has anything to do with that) I put a .dump on Amazon S3 and pushed it to my Heroku Postgres database. Any help would be great!

Relevant section from views.py:

@login_required 
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            if post.category == 'progresstracker':
                return redirect('books.views.pt_detail', slug=post.slug, category=post.category)
            elif post.category == 'resources':
                return redirect('books.views.resources')
            else:
                return redirect('books.views.bt_detail', slug=post.slug, category=post.category)
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})  

def pt_detail(request, slug, category):
    post = get_object_or_404(Post, slug=slug, category__slug=category)
    template = CATEGORY_TEMPLATES.get(post.category.slug)
    return render(request, template, {'post': post})  

The form:

{% extends 'blog/base.html' %}

{% block nav %}
    <li><a href="/home">Home</a></li>
    <li><a href="/progresstracker">Progress Tracker</a></li>
    <li class="dropdown">
        <a href="/blogtopics" class="dropbtn">Blog Topics</a>
        <div class="dropdown-content">
            <a href="/blogtopics/computer-science">Computer Science</a>
            <a href="/blogtopics/data-science">Data Science</a>
            <a href="/blogtopics/other">Other</a>
        </div>
    </li>
    <li><a href="/resources">Resources</a></li>
{% endblock %}


{% block content %}
<div id="content">
    <div class="padding">
        <h1>New post</h1>
            <form method="POST" class="progresstracker-form">{% csrf_token %}
            {{ form.as_p }}
            <button type="submit" class="save btn btn-default">Save</button>

            </form>

    </div>
</div>
{% endblock %}

The actual issue is the migrations, If you will take a look inside the heroku logs you will see a relation error for your model.

To solve this issue:

  1. First make sure you have added all the migrations to the git repo:

    git add <myapp>/migrations/* git commit -m "Fix Heroku deployment" git push heroku

  2. Then you need to migrate once again on Heroku by running these commands:

heroku run bash

~$  ./manage.py makemigrations
~$  ./manage.py migrate
~$  exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM