Django - check if object exists
<p>I'm trying to check if an object exists in my form but i'm getting an error. </p>

<p><strong>my views:</strong></p>

<pre><code>def foo(request):
    titulo = "something"
    form = MyForm(request.POST or None)
    queryset = myModel.objects.all()
    if myModel.objects.filter(name=name).exists():
        messages.error(request, 'already exists')
    context = {
        "titulo": titulo,
        "form": form,
        "queryset": queryset,
    }
    if form.is_valid():
        instance = form.save()
        messages.success(request, 'Added')
        return redirect('/')
    return render(request, "forms7.html", context)
</code></pre>

<p>Basically what I'm doing is adding an user, if the user exists I want a warning saying <code>already exists</code></p>

<p>Error:</p>

<blockquote>
  <p>UnboundLocalError at /model/ local variable 'name'
  referenced before assignment</p>
</blockquote>

<p>What am I doing wrong? </p>

<p><strong>Note:</strong>
If I add <code>name = 'jesus'</code> to my code and <code>jesus</code> exists it works but that's not how I want. </p>

<p>I want to check if the name I'm entering exists show me the msg </p>

<p>Could someone help me? thanks.</p>